Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constraint Checked On Missing Named Parameters #4987

Open
p6rt opened this issue Jan 3, 2016 · 3 comments
Open

Constraint Checked On Missing Named Parameters #4987

p6rt opened this issue Jan 3, 2016 · 3 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Jan 3, 2016

Migrated from rt.perl.org#127142 (status was 'new')

Searchable as RT127142$

@p6rt
Copy link
Author

p6rt commented Jan 3, 2016

From @zoffixznet

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

@p6rt
Copy link
Author

p6rt commented Jan 3, 2016

From @zoffixznet

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

@p6rt
Copy link
Author

p6rt commented Jan 3, 2016

From @zoffixznet

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.

@p6rt p6rt added the Bug label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant