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

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

Thread Previous | Thread Next
From:
Branislav ZahradnĂ­k
Date:
September 5, 2023 09:00
Subject:
Re: //= and ||= in subroutine signatures
Message ID:
CAB=rbOkzTeevjnqX28GStmsgaK_Wwo=BGEzMov=UqgdB7vUYxw@mail.gmail.com
On Sun, 3 Sept 2023 at 10:48, Ivan Vorontsov <ivrntsv@yandex.ru> 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.
>
>
you are mixing two domains not clearly formulated yet (in Perl)

one is signatures, where operators acts like "if not exists"
second is data contract, where you want "use 42 if undef"

first one struggles with missing concept of "not-exists" value
for second, you can use Type::Params for now, maybe current WIP (Oshun)
will solve it in future



> --
> Ivan Vorontsov <ivrntsv@yandex.ru>
>

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