Bug #15658
closedThe combination of non-Symbol keys and Symbol keys should be allowed
Description
It was prohibited at 2.6.0 and 2.6.1 to pass both non-Symbol keys and Symbol-keys simultaneously:
def foo(opt=nil, **kw)
p opt, kw
end
foo("str" => 42, :sym => 42)
#=> 2.5.3: {"str"=>42}, {:sym=>42}
#=> 2.6.0: non-symbol key in keyword arguments: "str" (ArgumentError)
However, at today's developers meeting, we found this change problematic. I'll revert the change soon, and will be backported to 2.6.2.
Rationale: To accept the mixed arguments, we can use an optional argument now:
def foo(opt={})
p opt
end
foo("str" => 42, :sym => 42) #=> works as intended: { "str" => 42, :sym => 42 }
However, this might not work in 3.0: it is now considered to completely separate keyword arguments from positional arguments (#14183). We will be able to use def foo(**opt)
in the situation in 3.0 because 3.0's **kw
parameter will allow non-Symbol key. However, if we want a code that works in both 2.5 and 3.0, we need "manual merge":
def foo(opt={}, **kw)
opt = opt.merge(kw)
...
end
However, this code does not work in 2.6.1 because it is prohibited.
#14183 is not determined yet, but anyway, we found that this prohibition required more careful consideration than we had expected. So, we cancel the prohibition once, and will reconsider the problem carefully.