Project

General

Profile

Actions

Bug #20041

open

Array destructuring and default values in parameters

Added by tenderlovemaking (Aaron Patterson) 5 months ago. Updated 24 days ago.


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) 5 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) 5 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) 5 months ago

  • Assignee set to tenderlovemaking (Aaron Patterson)
Actions #4

Updated by hsbt (Hiroshi SHIBATA) 24 days ago

  • Status changed from Open to Assigned
Actions

Also available in: Atom PDF

Like0
Like1Like0Like0Like0