-
Notifications
You must be signed in to change notification settings - Fork 22
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
Conversation
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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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;
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. |
There was a problem hiding this comment.
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)
I'm mildly opposed to simply returning
|
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 |
@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 package Some::Module v1.23;
use feature 'yield_true';
return "a different value"; |
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 |
I accidentally wrote this in Issues raised here have been addressed. Sorry 'bout that. |
On Sun, 3 Apr 2022, 17:44 Graham Knop, ***@***.***> wrote:
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.
That would be bad.
There is a difference between returning false and dieing during require.
Returning false allows the code to be required again, possibly after fixing
the cause of the false return, dieing does not allow the file to be loaded
again. (Fixing part of this, where death is signified by a specific undef,
is on my todo list.)
Therefore this feature must distinguish and deal with modules which
explicitly return false.
Yves
… |
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 |
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 withuse feature 'neo';
because "Neo is the One", but it's terrible name, even though it amuses me :)