Paul "LeoNerd" Evans
2024-08-13 21:45:03 UTC
We have lexical subs. They're nice for keeping namespaces clean
my sub f ( $x, $y ) { ... }
say "The value of f is ", f(123, 456);
# calls the lexical f() even though there is no `sub f` visible in
# the package
Various discussions around object system ideas have been inspired the
idea that it'd be nice to be able to do this with methods as well.
Having lexically-scoped methods that are not visible outside the
lexical scope helps keep namespaces clean - doubly useful around object
classes where you expect external callers to call methods.
Already this is achievable by storing an anonymous method into a normal
lexical variable, in the same way you could have done that in perls
prior to 5.18 with subs. But it doesn't look very neat:
class C {
my $inner = method { ... };
method outer { $self->$inner() }
}
It's also potentially problematic for a bunch of upcoming-feature
reasons to simply have an anonymous method expression sitting around in
a lexical variable like that. Really, it wants to be properly supported
first-class syntax that the language properly knows about.
What we want is some kind of ability to write `my method ...` and be
able to call it.
We did briefly think about simply allowing that as part of normal
method resolution:
class C {
my method inner { ... }
method outer { $self->inner() }
}
However, this might be considered "surprising" by many. It's no more
surprising than lexical subs already are currently; but maybe that's
surprising enough as it is?
Another thought would be to have different notation at the callsite, to
make it obvious this is a lexical lookup. One idea was the `->&NAME`
arrow notation.
class C {
my method inner { ... }
method outer { $self->&inner() }
}
I've already got quite a few PPC documents open already, plus a few
more I'm in the middle of writing, so I'm kindof loathe to write yet
another one, but what does anyone think?
Moreover - does anyone feel sufficiently strongly about this idea that
they'd actually comment and contribute to such a PPC discussion? Or
should I just go ahead and skip straight to implementing and raising a
PR for it?
my sub f ( $x, $y ) { ... }
say "The value of f is ", f(123, 456);
# calls the lexical f() even though there is no `sub f` visible in
# the package
Various discussions around object system ideas have been inspired the
idea that it'd be nice to be able to do this with methods as well.
Having lexically-scoped methods that are not visible outside the
lexical scope helps keep namespaces clean - doubly useful around object
classes where you expect external callers to call methods.
Already this is achievable by storing an anonymous method into a normal
lexical variable, in the same way you could have done that in perls
prior to 5.18 with subs. But it doesn't look very neat:
class C {
my $inner = method { ... };
method outer { $self->$inner() }
}
It's also potentially problematic for a bunch of upcoming-feature
reasons to simply have an anonymous method expression sitting around in
a lexical variable like that. Really, it wants to be properly supported
first-class syntax that the language properly knows about.
What we want is some kind of ability to write `my method ...` and be
able to call it.
We did briefly think about simply allowing that as part of normal
method resolution:
class C {
my method inner { ... }
method outer { $self->inner() }
}
However, this might be considered "surprising" by many. It's no more
surprising than lexical subs already are currently; but maybe that's
surprising enough as it is?
Another thought would be to have different notation at the callsite, to
make it obvious this is a lexical lookup. One idea was the `->&NAME`
arrow notation.
class C {
my method inner { ... }
method outer { $self->&inner() }
}
I've already got quite a few PPC documents open already, plus a few
more I'm in the middle of writing, so I'm kindof loathe to write yet
another one, but what does anyone think?
Moreover - does anyone feel sufficiently strongly about this idea that
they'd actually comment and contribute to such a PPC discussion? Or
should I just go ahead and skip straight to implementing and raising a
PR for it?
--
Paul "LeoNerd" Evans
***@leonerd.org.uk
http://www.leonerd.org.uk/ | https://metacpan.org/author/PEVANS
Paul "LeoNerd" Evans
***@leonerd.org.uk
http://www.leonerd.org.uk/ | https://metacpan.org/author/PEVANS