-
-
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
next
accepts named arguments
#6394
Comments
That's also happening at |
Additionally |
If no positional arguments are present, named arguments could perhaps be transformed to a named tuple. So |
All of this works in Ruby, I would do nothing regarding this for now. |
foo = while true
break 1, foo: 1
end
p foo In Ruby it yields |
Wow! Crystal ignore |
I just checked and #10566 did not resolve this one. |
#10566 is only concerned with how Ruby's behaviour here is to combine all named arguments, including from double splats (analogous to #10193), into a hash as the last value. But then there is the following oddity: def foo(**opts)
yield **opts
end
foo { |x| x } # => {}
foo { |*x| x } # => [] In Ruby 3 the first line will return I am not sure which behaviour we would choose, or even whether we need them. IMO it shouldn't be possible to mix positional arguments and named arguments in the same parameter: def foo
yield 1, 2
yield 3, a: 4
end
foo { |_, y| typeof(y) } # => (Int32 | NamedTuple(a: Int32)) |
I would just make named arguments from these keywords an error and be done with it. I don't see much usefulness for using named arguments with |
For def foo
yield a: 1, b: 2
end
foo do |b:, **ns|
[b, ns] # => [2, {:a=>1}]
end
def bar
yield 1, 2, 3, 4, a: 5, b: 6, c: 7, d: 8
end
bar do |x0, x1, *xs, b:, a:, **ns|
[x0, x1, xs, b, a, ns] # => [1, 2, [3, 4], 6, 5, {:c=>7, :d=>8}]
end A Crystal analogue would be: def foo
yield a: 1, b: 2
end
foo do |*, b, **ns|
{b, ns} # => {2, {a: 1}}
end
def bar
yield 1, 2, 3, 4, a: 5, b: 6, c: 7, d: 8
end
bar do |x0, x1, *xs, b, a, **ns|
{x0, x1, xs, b, a, ns} # => {1, 2, {3, 4}, 6, 5, {c: 7, d: 8}}
end However this is very tricky to implement because it implies all |
Arguments to
next
are parsed like arguments to method calls, but they shouldn't support named parameters:This should probably raise a syntax error. Could also capture the named arguments and provide them somehow to the yielding scope, but that's probably too complicated - and not really useful.
If only named arguments are provided
next foo: 2
, it raisesIndex out of bounds
.The text was updated successfully, but these errors were encountered: