Front page | perl.perl5.porters |
Postings from June 2022
Re: goto and @_
Thread Previous
From:
Graham Knop
Date:
June 5, 2022 13:28
Subject:
Re: goto and @_
Message ID:
CAM=m89GEH1P60vRR2Qdm2C-GT9fCRfgi78TrKMQy1a6CQqTJJQ@mail.gmail.com
On Fri, Jun 3, 2022 at 9:55 PM Ovid <curtis.poe@gmail.com> wrote:
>
> Thanks Paul!
>
> Not quite sure I like this very single-use function (where else might people try to call it and how will this go wrong?), but I have no better suggestion.
>
> While we're at it, I know you've discussed multi subs and this is even more interesting to me now (even though I've always favored the idea).
>
> I had this code:
>
> $method => sub ( $self, $value = undef ) {
> _debug( "Args for overloaded reader/writer for $name", \@_ );
> return @_ == 1
> ? $self->$reader_method
> : $self->$writer_method($value);
> }
>
> But haarg pointed out that the intent was for @_ to go away in signatured subs. This was the best I could think of:
>
> $method => sub ( $self, @value ) {
> _debug( "Args for overloaded reader/writer for $name", [ $self, @value ] );
> return @value == 0
> ? $self->$reader_method
> : $self->$writer_method(@value);
> }
>
> I'm not passing an array or a list as the second argument. Instead, I'm stuck pretending that I am and I should have a check for @value having more than 1 argument. In short, variadic subs aren't handled well by signatures. If we had multidispatch, much of this ugliness would go away:
>
> sub some_method :multi ($self) { ... }
> sub some_method :multi ($self, $new_value) { ... }
>
> But that doesn't address how I'd create an anonymous multisub.
>
> So far, I'm loving signatures, but now that I'm converting some code to deal with the impending removal of @_, I'm less convinced.
>
> Now that I think about it, with a decent introspection mechanism for sub arguments (including the ability to set them), the tailcall issue would also go away (and the potential bugs of a single-use function with it) and the code I just wrote would be much cleaner. I hate @_ because it's just more punctuation to confuse new developers, but it does what it's supposed to do quite well. I'm open to suggestions.
I'd probably suggest avoiding signatures for subs that need this type
of introspection. In the future there will hopefully be better
introspection abilities with signatures, but until then, the old style
of argument handling using @_ still works perfectly fine. Using goto
to remove a stack frame is already an uncommon case that won't be
obvious to a new developer.
There have been some proposals on how this could be improved in the
future. In particular see Dave Mitchell's posts regarding signature
extensions: https://www.nntp.perl.org/group/perl.perl5.porters/2019/11/msg256677.html
In particular, the post about Query Parameters:
https://www.nntp.perl.org/group/perl.perl5.porters/2019/11/msg256680.html
Thread Previous