Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New RFC: "yield_true" at end of compilation #14

Closed
wants to merge 1 commit into from
Closed

New RFC: "yield_true" at end of compilation #14

wants to merge 1 commit into from

Conversation

Ovid
Copy link

@Ovid Ovid commented Apr 3, 2022

This RFC is to eliminate the need for a magic true value (usually a hard-coded trailing 1) being required in code we use.

I guess as to the RFC number. If there is a process for assigning them, I've missed it. My apologies.

Better names than yield_true are welcome. I was tempted to go with use feature 'neo'; because "Neo is the One", but it's terrible name, even though it amuses me :)

@Ovid Ovid changed the title Add RFC for "yield_true" New RFC: "yield_true" at end of compilation Apr 3, 2022
@haarg
Copy link
Contributor

haarg commented Apr 3, 2022

It is possible for a file to explicitly return a false value. I'd expect that would just be ignored with this feature enabled, but it's probably worth specifying.

## Abstract

This RFC proposes a `yield_true` feature. When used, the current Perl _file_
containing `use feature 'yield_true'l` will automatically return a true value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo, you probably didn't mean the "1" in here


## Future Scope

I believe this is complete.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention of having it be a named feature, is that a future version bundle will include it, so you'll get it automatically with, perhaps,

use v5.40;

@haarg
Copy link
Contributor

haarg commented Apr 3, 2022

A prototype implementation exists at https://metacpan.org/pod/true


use feature 'yield_true';

Code using the above does not need to return a magic true value when compiled.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something to think about is that while most modules just put "1" or some other dummy true value, there are some modules that make use of the return value, because it becomes the value returned by a require. In particular, it is not uncommon to yield the package name itself, when that is an object class, so you can do things like

my $obj = (require Some::Object::Class)->new;

I don't think we want to complicate this feature idea by suggesting we'd allow customisation of the true value being returned, but perhaps if we're returning some value anyway, the string form of the package name is just as good as any, and makes this use-case still work. (And anyone who writes package 0 deserves what they get :P)

@Corion
Copy link

Corion commented Apr 3, 2022

I'm mildly opposed to simply returning 1 because I still entertain the idea of actually (optionally) returning an object. Returning the package name is a good thing though. The return value of require My::Example; should always return the package name, even on the second invocation then:

for (1..2) {
    my $factory = require My::Example;
    my $handler = $factory->new($_);
}

@haarg
Copy link
Contributor

haarg commented Apr 3, 2022

Changing the return value of require seems like a separate issue from what this RFC wants to address.

If you wanted require to always return the same value, and for that value to come from the file, you need a new location to store these values. This would probably mean a new superglobal to go along with %INC. And it would usually be useless because most modules return 1. I don't think this is a very good idea.

If you wanted require to always return the package name, it's a separate issue from this RFC because that means ignoring the return value from the file. It also presents a problem because require doesn't actually take package names. It takes file path fragments. Foo::Bar is translated to "Foo/Bar.pm" at parse time. This would then need to be converted back to a package name, or do something else. I don't think this is a good idea either.

Instead, it's probably best addressed with a builtin::load or similar routine that accepts a package as a string. This has been discussed in the past, and solves other problems. Module::Runtime has a use_module function that behaves like this, returning the package name.

@leonerd
Copy link
Contributor

leonerd commented Apr 3, 2022

@haarg makes some good points about returning odd values, so yes probably best to stick to just "1" (or more likely the new "true" value)

However, in saying the word "return", does raise an interesting point. Most modules currently do not return a true value, they simply yield one by being the final expression in the block. It is possible to explicitly return a value. Presumably this is good enough to override whatever default-true value this feature would propose.

package Some::Module v1.23;
use feature 'yield_true';

return "a different value";

@haarg
Copy link
Contributor

haarg commented Apr 3, 2022

Would returning a false value also override the true value? It feels a bit strange to me. Maybe an explicit return in the context of yield_true should be an error.

@Ovid
Copy link
Author

Ovid commented Apr 3, 2022

I accidentally wrote this in master instead of a branch. Closing this in favor of #16

Issues raised here have been addressed. Sorry 'bout that.

@Ovid Ovid closed this Apr 3, 2022
@demerphq
Copy link

demerphq commented Apr 4, 2022 via email

@haarg
Copy link
Contributor

haarg commented Apr 4, 2022

I'm not convinced that is a behavior that needs to be preserved under this feature. Allowing a module to be required multiple times is mostly a surprising behavior, and anything expecting to handle that programmatically would need to either be working with known modules or need some external way of communicating what is needed to load the module. And if the caller knows it can attempt loading the module multiple times, it can remove the %INC entry to allow it. Or, the module in question can disable (or leave disabled) this feature.

rjbs pushed a commit to rjbs/RFCs that referenced this pull request Sep 25, 2022
rjbs pushed a commit that referenced this pull request Oct 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants