develooper Front page | perl.perl5.porters | Postings from November 2019

Re: Parameter Attributes

Thread Previous
From:
Paul "LeoNerd" Evans
Date:
November 30, 2019 15:20
Subject:
Re: Parameter Attributes
Message ID:
20191130152011.0aa82de0@shy.leonerd.org.uk
On Thu, 28 Nov 2019 17:01:00 +0000
Dave Mitchell <davem@iabyn.com> wrote:

> Note that in Perl 6 and some CPAN signature modules, the 'method'
> keyword declares an implicit $self parameter, whose name can be
> overridden using a postfix ':':
> 
>     method foo($x, $y)      { $self->{$x} = $y }  # implicit $self
>     method foo($me: $x, $y) {   $me->{$x} = $y }  # explicit invocant
> 
> I have no plans to introduce such a 'method' keyword, but if we did,
> we might need different syntax for the invocant, as the ':' would be
> interpreted as the start of an attribute unless the toker was clever
> and we are very careful that all signature syntax is capable of being
> disambiguated.

That should be fine - I don't see a need to offer users ability to
rename the `$self` anyhow.

In my "Object::Pad" experiment on CPAN, playing around with what ideas
that sort of `method` keyword might add, I didn't stop to consider
letting users change the invocant variable name; there's nothing that
can be gained by adding it as compared all the complexity of
implementing it. Even the most contrived example, that of a (named)
method generating a method closure which still wants to refer to the
outer $self, can be obtained by making a new lexical for the purpose:

  method generate_closure() {
    my $outer_self = $self;
    return method {
      say "My invocant is $self but outer was $outer_self";
    };
  }

I think that's clear enough for those rare cases, and avoids any
complication that comes from trying to offer customisation of the $self
lexical.

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About