rbs icon indicating copy to clipboard operation
rbs copied to clipboard

Array(T | Array[T]) not well handled

Open HoneyryderChuck opened this issue 1 year ago • 2 comments

Given a variable typed as smth or collection of smth, Array(a) does not spread correctly:

# a: Symbol | Array[Symbol]
Array(a).each do |el|
# el is typed as Symbol | Array[Symbol], instead of Symbol

HoneyryderChuck avatar Jun 07 '24 16:06 HoneyryderChuck

The problem is once again https://github.com/ruby/rbs/blob/45b34bd9bf9a2b6872ae832f6a344b23dc75ec3d/core/kernel.rbs#L442-L444 Contray to our expectation, former overloads does not narrow latter overloads.

We’d have to change that to something like [T] (T ele) -> [T] | Array[untyped], yet this still won’t help with the topic without more efforts done by the type checkers.

ParadoxV5 avatar Jun 07 '24 19:06 ParadoxV5

Confirmed. The type of Array(a) is Array[Symbol | Array[Symbol]] where it is expected to be Array[Symbol]. Not sure if I can fix the problem... 😫

soutaro avatar Jun 14 '24 01:06 soutaro