Union of missing and number

This is related to a SO question (here):

Why does this work:

foo(x::Vector{Union{Missing, T}}) where T<:Number = sum(collect(skipmissing(x)))
foo([1, 2, missing])
>> 3

but not this

bar(x::Vector{Union{Missing, <:Number}}) = sum(collect(skipmissing(x)))
bar([1, 2, missing])
ERROR: MethodError: no method matching bar(::Array{Union{Missing, Int64},1})
Closest candidates are:
  bar(::Array{Union{Missing, #s1} where #s1<:Number,1}) at REPL[58]:1
Stacktrace:
 [1] top-level scope at none:0

The where is in a different “scope”.

foo(x::Vector{Union{Missing, T}}) where T<:Number 
# vs
foo(x::Vector{Union{Missing, T} where T<:Number})
3 Likes

That makes sense, it seems to me it’s the kind of slightly tricky thing that may benefit from being in the doc, do you know if there’s something about such “scopes”? thanks!

1 Like