develooper Front page | perl.perl5.porters | Postings from September 2023

Re: //= and ||= in subroutine signatures

Thread Previous
From:
Ovid
Date:
September 3, 2023 10:12
Subject:
Re: //= and ||= in subroutine signatures
Message ID:
CA+M4CHs4kVgTjhYnjeWghoSJWsP-a16DjUG668AT03QPNhRs_g@mail.gmail.com
On Sun, Sep 3, 2023 at 11:32 AM Dave Mitchell <davem@iabyn.com> wrote:

> On Sun, Sep 03, 2023 at 11:48:14AM +0300, Ivan Vorontsov wrote:
> > With perl v5.38.0 I wrote a code similar to:
> >     perl -E 'sub foo ($x, $y //= 42, $z) {} foo(1, undef, 3)'
> > and got the error:
> >     Mandatory parameter follows optional parameter at -e line 1, near
> "$z) "
> >
> > So //= and ||= mark a parameter as optional, but optional parameters
> > must follow mandatory ones. Outside of signatures these operators check
> > a value of a variable. A mandatory parameter can have undef or false
> > value. If it is feasible, maybe these operators could be applied to
> > mandatory parameters as well.
>
> I don't see how that could work. It makes for an ambiguity.


I generally find that if I need more than two parameters and some are
optional, switching to named parameters fixes the issue. It also makes it
easier to distinguish between the cases of "not passed" and "undef passed."

Best,
Ovid
-- 
Curtis "Ovid" Poe
--
CTO, All Around the World
World-class software development and consulting
https://allaroundtheworld.fr/

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