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

Why do defaults on slurpies throw? #2440

Open
zoffixznet opened this issue Oct 28, 2018 · 8 comments
Open

Why do defaults on slurpies throw? #2440

zoffixznet opened this issue Oct 28, 2018 · 8 comments
Labels
consensus needed Needs a well-versed decision with justification, possibly from a core developer

Comments

@zoffixznet
Copy link
Contributor

zoffixznet commented Oct 28, 2018

An optional % or @ sigiled param defaults to an empty list. You can also give it a default value:

<Zoffix__> m: m: -> @a? { dd @a }()
<camelia> rakudo-moar 266af373d: OUTPUT: «Array element = []␤»
<Zoffix__> m: m: -> @a = <default list> { dd @a }()
<camelia> rakudo-moar 266af373d: OUTPUT: «("default", "list")␤»

And while a slurpy param also defaults to an empty list, you cannot give it a default value:

<Zoffix__> m: m: -> *@a { dd @a }()
<camelia> rakudo-moar 266af373d: OUTPUT: «Array element = []␤»
<Zoffix__> m: m: -> *@a = <default list> { dd @a }()
<camelia> rakudo-moar 266af373d: OUTPUT: «===SORRY!=== Error while compiling <tmp>␤Cannot put default on slurpy parameter @a␤at <tmp>:1␤------> m: -> *@a = <default list>⏏ { dd @a }()␤    expecting any of:␤        constraint␤        infix stopper␤»

What's the reason for this limitation? It looks to be fairly useful when you want both the flattening of slurpies and some default values to work with.


There was propspec codifying this behaviour in 6.d and I moved it to APPENDIXES at least for now: Raku/roast@f7d9be8ad9

@zoffixznet zoffixznet added the consensus needed Needs a well-versed decision with justification, possibly from a core developer label Oct 28, 2018
zoffixznet added a commit to Raku/roast that referenced this issue Oct 28, 2018
It's a useful behaviour to have and I don't understand why
it must throw instead: rakudo/rakudo#2440
@lizmat
Copy link
Contributor

lizmat commented Oct 28, 2018 via email

@zoffixznet
Copy link
Contributor Author

I think the original reasoning is very simple: having no values for a slurpy is perfectly valid.

Having no values for an optional @-/%- sigilled param is also valid, and you can have a default too

@vendethiel
Copy link
Contributor

I think it's because a slurpy is never "not given". A param, $@% can simply be not passed, but a slurpy will always be "passed".

@zoffixznet
Copy link
Contributor Author

but a slurpy will always be "passed".

Not really, I'm not "passing" anything. The callsite to a routine with optional @-param looks the same as the one with a slurpy.

  • Some arguments may be used to fill an optional @- parameter
  • Some arguments may be used to fill a slurpy @- parameter

Parallels with

  • No arguments to fill an optional @- parameter are given, use default
  • No arguments to fill a slurpy @- parameter are given, use default

There's no ambiguity to the decision of when a slurpy should be given its default value, other than possible implementation detail in rakudo.

On language-level, the decision to block slurpy defaults make no sense to me.

@vendethiel
Copy link
Contributor

vendethiel commented Oct 29, 2018

No arguments for a positional (with no default) is invalid.
No arguments for a slurpy is totally valid.

@zoffixznet
Copy link
Contributor Author

No arguments for a positional (with no default) is invalid.
No arguments for a slurpy is totally valid.

So what?

@zoffixznet
Copy link
Contributor Author

So what?

As in, I don't see the relevance of mandatory parameters to slurpies. You'll still have a total valid slurpy with no arguments if defaults are allowed. In fact, the current behaviour can be explained with a paragidm where a slurpy always has a default value: an empty Array/List.

And the current propspec says that default value must not be configurable by the user, to which I ask: "why?"

ugexe pushed a commit to Raku/roast that referenced this issue Dec 15, 2018
It's a useful behaviour to have and I don't understand why
it must throw instead: rakudo/rakudo#2440
@2colours
Copy link
Contributor

2colours commented Jan 3, 2023

@zoffixznet for what it's worth, I think the counterargument could be put in a different light the following way: what can one do to make the slurpy an empty list, if no values were to trigger the default value, rather than the empty list?

And this is where things start to escalate quickly... a missing value is reflected with an empty array for an optional @var parameter as well (named or positional) - however, they don't use the empty array (or even Empty, for that matter) to indicate the absence of a value. The concepts are distinct.

I'm not sure if the same distinction could be preserved(/achieved in the first place) in any way for a slurpy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
consensus needed Needs a well-versed decision with justification, possibly from a core developer
Projects
None yet
Development

No branches or pull requests

4 participants