Skip Menu |
Report information
Id: 127142
Status: new
Priority: 0/
Queue: perl6

Owner: Nobody
Requestors: cpan [at] zoffix.com
Cc:
AdminCc:

Severity: (no value)
Tag: Bug
Platform: (no value)
Patch Status: (no value)
VM: (no value)



Subject: [BUG] Constraint Checked On Missing Named Parameters
Download (untitled) / with headers
text/plain 1.1k
If a constraint is created on a named parameter with either with a `where` or with a `subset` and that named parameter is NOT present in the call of the subrouting, the constraint check still occurs. This makes it impossible to have optional constrained named parameters. Here's the output showing (1) successfull use of optional named parameter without constraint; (2) error with `subset` constraint. (3) error with `where` constraint: <Zoffix> m: sub foo (Int :$x){}; foo <camelia> rakudo-moar 3734ea: ( no output ) <Zoffix> m: subset Foo of Mu where $_ < 42; sub foo (Foo :$x){}; foo <camelia> rakudo-moar 3734ea: OUTPUT«Cannot call infix:«<»(Mu, Int); none of these signatures match:␤ ($?)␤ (\a, \b)␤ (Real \a, Real \b)␤ (Int:D \a, Int:D \b)␤ (int $a, int $b)␤ (Num:D \a, Num:D \b --> Bool)␤ (num $a, num $b --> Bool)␤ (Rational:D \a,…» <Zoffix> m: sub foo (:$x where Int){}; foo <camelia> rakudo-moar 3734ea: OUTPUT«Constraint type check failed for parameter '$x'␤ in sub foo at /tmp/I02EogbpEg line 1␤ in block <unit> at /tmp/I02EogbpEg line 1␤␤» Relevant IRC conversation: http://irclog.perlgeek.de/perl6/2016-01-03#i_11823741
Download (untitled) / with headers
text/plain 554b
Show quoted text
> This makes it impossible to have optional constrained named parameters.
There is a way, but it's ugly, unintuitive, and differs from what is needed when the parameter is non-optional. <Zoffix> m: sub foo(:$x where { !$_.defined || $_ < 42 }) {}; foo <camelia> rakudo-moar 3734ea: ( no output ) Also, the issue exists with optional positional parameters as well: <Zoffix> m: sub foo($x? where $_ < 42 ) {}; foo <camelia> rakudo-moar 3734ea: OUTPUT«Use of uninitialized value of type Any in numeric context in sub foo at /tmp/562mGzNaCQ line 1␤»
Download (untitled) / with headers
text/plain 247b
Show quoted text
> There is a way, but it's ugly, unintuitive, and differs from what is > needed when the parameter is non-optional.
Errr, (no edit button), the way shown above is not really a solution as it misses when the argument IS specified, but not defined.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org