develooper Front page | perl.perl5.porters | Postings from March 2021

Re: De-experimentalising "signatures"

Thread Previous | Thread Next
From:
ilmari
Date:
March 22, 2021 18:15
Subject:
Re: De-experimentalising "signatures"
Message ID:
87k0pzoy1y.fsf@wibble.ilmari.org
"Paul \"LeoNerd\" Evans" <leonerd@leonerd.org.uk> writes:

> On Mon, 22 Mar 2021 17:12:19 +0100
> Leon Timmermans <fawaka@gmail.com> wrote:
>
>> That does sound reasonable. I'm not sure why we haven't done so
>> already; that would have prevented this situation.
>
> Without @_ you can't do arity-checking. E.g. while I personally dislike
> the style, a popular thing to do with object accessors is something
> like:
>
>   sub name {
>     my $self = shift;
>     $self->{name} = shift if @_;
>     return $self->{name};
>   }
>
> That could be written
>
>   sub name ($self, $new = undef) {
>     $self->{name} = $new if @_ > 1;
>     return $self->{name};
>   }

Because the default value can be any term, and only gets evaluated if
there is no value passed, this works (not that I endorse it):

    package Foo {
        sub new { bless {}, shift }

        sub bar($self, $bar = return $self->{bar}) {
            $self->{bar} = $bar;
        }
    }

    my $foo = Foo->new;
    $foo->bar(42);
    say $foo->bar;  # 42


> (or something neater with Object::Pad + `method`), but lacking access
> to any form of scalar-snail, means we can't distinguish
>
>   $obj->name()
>
> from
>
>   $obj->name(undef)

This works with Object::Pad too:

    class Foo {
        has $bar;

        method bar($_bar = return $bar) {
            $bar = $_bar;
        }
    }

    my $foo = Foo->new;
    $foo->bar(42);
    say $foo->bar; # 42


> This is one among a number of reasons why @_ is currently still
> required in some situations, for which there are designs on adding more
> bits to signatures which will make simpler.

But the majority of subs with signatures will not need it, which is why
I'm still of the opinion that a `snail` feature that can be enabled or
disabled orthogonally of signatures is the way forward for that.

Whether we disable `snail` in the same feature bundles that enable
`signatures`, or wait until there's a `signatures::v2` that includes
predicate syntax is a separate discussion.

- ilmari
-- 
- Twitter seems more influential [than blogs] in the 'gets reported in
  the mainstream press' sense at least.               - Matt McLeod
- That'd be because the content of a tweet is easier to condense down
  to a mainstream media article.                      - Calle Dybedahl

Thread Previous | Thread Next


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