-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Splat operator doesn't work outside parameter list. #1573
Comments
...so all it does is construct a one-element list? |
In Crystal splat operates on tuples, not arrays and it's strictly limited to the call and argument list syntax. This one may be fixable, though it's already quite debatable whether it should create an array or a tuple. However the above gives a quite clear scope to the splat operator in Crystal and there are many places where it's valid in Ruby that Crystal simply can't support (see #1464 for example). So that would just make its definition less clear. |
@kirbyfan64, Actually it can do a lot more http://www.monkeyandcrow.com/blog/the_strange_ruby_splat/ |
But it's not possible. |
I'm closing this as a kind of duplicate of #132. Basically, allow the splat operator to be used in more ways and more places. |
We shouldn't close this because #132 deals exclusively with splat collections on the LHS and this one is about splat expansions on the RHS. In Ruby, these: ... = *exp1
... = exp1, exp2
... = *exp1, exp2
... = exp1, *exp2 behave identically to: ... = [*exp1]
... = [exp1, exp2]
... = [*exp1, exp2]
... = [exp1, *exp2] We could do a similar syntactic transformation whenever the RHS consists of anything but a single non-splat expression: ... = ::Tuple.new(*exp1)
... = ::Tuple.new(exp1, exp2)
... = ::Tuple.new(*exp1, exp2)
... = ::Tuple.new(exp1, *exp2) With #3718, we can also express this in terms of the tuple literal: ... = {*exp1}
... = {exp1, exp2}
... = {*exp1, exp2}
... = {exp1, *exp2} Note that if we transform the RHS like this then many-to-many assignments must be interpreted differently: # in the same way we allow:
x, y = {1, 2, 3}
{1, 2, 3}.try { |x, y| }
# we should also permit:
x, y = 1, 2, 3
# it will expand to:
__temp = ::Tuple.new(1, 2, 3) # or tuple literal
x = __temp[0]
y = __temp[1]
__temp
# the following still errors, but the message won't be "Multiple assignment count mismatch":
x, y, z = 1, 2 # Error: index out of bounds for Tuple(Int32, Int32) (2 not in -2..1) The trailing We still won't allow splat expansions of non- |
It's true that it's different from the other issue. But this feature is just being able to omit curly braces for tuples. I don't think that's very useful. I prefer to keep the language smaller. I'm pretty sure, if you ask most rubyists, what the splat operator does in these cases, they will not know and they will be confused. In fact the splat operator alone might already be confusing for a lot of programmers. |
Ruby:
Crystal:
I believe, splat operator must behave exactly like in Ruby (outside parameters), or absent at all, as no need to do the same stuff using different syntax constructions.
The text was updated successfully, but these errors were encountered: