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

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

Thread Previous | Thread Next
From:
Ivan Vorontsov
Date:
September 5, 2023 21:37
Subject:
Re: //= and ||= in subroutine signatures
Message ID:
20230906003713.9e192bfc4e07fd1c6f6be67c@yandex.ru
Let me try to answer this message one more time. Consider this answer
independent from the other one that took a different route.

On Mon, 4 Sep 2023 14:10:56 +0100 Dave Mitchell <davem@iabyn.com> wrote:

> On Sun, Sep 03, 2023 at 01:28:08PM +0300, Ivan Vorontsov wrote:
> > Maybe genuine optional parameters must be only the ones marked by =
> > sign. Others are mandatory. To mandatory parameters with //= or ||=
> > operators, that are last in the list or precede optional ones with =,
> > undef values are assigned, if there are less arguments than mandatory
> > parameters, and then operator works with that value.
> 
> I think that that's complex and confusing. You're basically saying
> 
>     f($x, $y = 1,    $z = 2)    # $y is optional parameter
>     f($x, $y //= 1,  $z = 2)    # $y is optional parameter
>     f($x, $y //= 1,  $z    )    # $y is mandatory parameter
>     f($x, $y //= 1         )    # $y is optional parameter

With my suggestion it should be:

    f($x, $y = 1,    $z = 2)    # $y is optional parameter
    f($x, $y //= 1,  $z = 2)    # $y is mandatory parameter
    f($x, $y //= 1,  $z    )    # $y is mandatory parameter
    f($x, $y //= 1         )    # $y is mandatory parameter

Consider this example:

    f($v, $w //= 42, $x, $y //= 1, $z = 2)

Here $v, $w, $x, $y are mandatory, $z is optional. But function can be
called with less arguments than number of mandatory parameters because
of $y. In that case $y assigned undef, then checked assignment is
applied. When number of arguments is equal to number of mandatory
parameters only checked assignment is applied to $y. Checked assignment
is applied to $w always.

But with my suggestion = parameters must be very last in the parameter
list without //= and ||= parameters in between them. That's a drawback.
Right now they can be interspersed.

> I think //= and ||= should be consistently either mandatory or optional,
> and not vary based on context.

Mandatory by the suggestion.

> And since there already been a release (5.38) where they're optional, I
> think we should keep it that way.

As I understand unconditionally. So any change is a break. And a user
visible break as far as I see is the prohibition of interspersing =
parameters with //= and ||= parameters. Allowing interspersing
mandatory parameters with //= and ||= parameters is an addition, not a
break. It brings questions. Is the suggestion worthwhile the break? Is
it implementable?

This is my last attempt on this idea. I hope I am not too irritating.

-- 
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