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

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

Thread Previous | Thread Next
From:
"Ruud H.G. van Tol" via perl5-porters
Date:
September 6, 2023 07:26
Subject:
Re: //= and ||= in subroutine signatures
Message ID:
31537f6b-6159-426e-89fa-7eb68e4d38a5@isolution.nl

On 2023-09-05 23:37, Ivan Vorontsov wrote:
> [...]
>      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.

Mandatory is a strict condition,
so if-or-when it gets called with less arguments than defined as the 
minimum,
then that is an error, which is best caught at compile-time ("if") already.

A run-time ("when") check is still needed, as f(@v) could turn out to be 
a violation.

-- Ruud


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