Bug #20041
openArray destructuring and default values in parameters
Description
It's possible to set the default value of a parameter to a previous parameter. For example:
def foo(a, b = a)
b
end
foo([1, 2]) # => [1, 2]
However, if the parameters are destructured, the destructring happens after default parameter assignment. For example:
def foo((x, y), b = x)
[x, y, b]
end
foo([1, 2]) # => [1, 2, nil]
Is this expected behavior? I would have expected the parameters to be "evaluated" from left to right, and the array destructuring to happen before the default parameter assignment.
Thanks!
Updated by matz (Yukihiro Matsumoto) 11 months ago
It is not intended behavior, but under the current implementation, fixing this issue would make the VM far more complex, so we left this behavior untouched, when we found this for the first time. If someone come up with the nice idea, we will reconsider.
Matz.
Updated by tenderlovemaking (Aaron Patterson) 11 months ago
matz (Yukihiro Matsumoto) wrote in #note-1:
It is not intended behavior, but under the current implementation, fixing this issue would make the VM far more complex, so we left this behavior untouched, when we found this for the first time. If someone come up with the nice idea, we will reconsider.
Matz.
Sounds good, thank you. I think I have an idea, but it'll take time for the patch.
Updated by tenderlovemaking (Aaron Patterson) 11 months ago
- Assignee set to tenderlovemaking (Aaron Patterson)
Updated by hsbt (Hiroshi SHIBATA) 7 months ago
- Status changed from Open to Assigned