📑
[Feature #21047] *nil のときに nil.to_a を呼び出さないようにする提案
[Feature #21047] Change *nil to not call nil.to_a, for consistency with **nil
-
*nilではnil.to_aが内部で呼び出されますが**nilはnil.to_hやnil.to_hashは呼び出されません
class NilClass
prepend Module.new {
def to_a
pp "#to_a"
super
end
def to_h
pp "#to_h"
super
end
}
end
[*nil] # => "#to_a"
[**nil] # => なし
- これが一貫性がないので一貫性を保つために
nil.to_aを呼び出さないようにする提案 -
**nilがto_h, to_hashを呼び出さないようにして最適化されているので*nilでも同様に最適化したい、みたいなところもモチベーションみたいですね - この対応は開発版の Ruby 3.5-dev で対応済ですね
class NilClass
prepend Module.new {
def to_a
pp "#to_a"
super
end
}
end
[*nil]
# Ruby 3.4 => "#to_a"
# Ruby 3.5 => なし
- あんまり実コードでは影響はないと思うのですが注意する必要があるかもしれませんね
Discussion