Error by `append(a::Vector{T}, b::Vector{T})::Vector{T} where T = vcat(a, b)`

The following shorter function definition gives the error but the longer definition doesn’t:

julia> append(a::Vector{T}, b::Vector{T})::Vector{T} where T = vcat(a, b)
ERROR: UndefVarError: `T` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
Stacktrace:
 [1] top-level scope
   @ REPL[1]:1
julia> function append(a::Vector{T}, b::Vector{T})::Vector{T} where T
           vcat(a, b)
       end
append (generic function with 1 method)

I’m not sure why the former gives the error. Could someone please tell me why this is happening?

I think it has to do with how the function is parsed.

ex1 = Meta.parse("append(a::Vector{T}, b::Vector{T})::Vector{T} where T = vcat(a, b)")


julia> ex1.args[1].head

:(::)

julia> ex1.args[1].args

2-element Vector{Any}:

:(append(a::Vector{T}, b::Vector{T}))

:(Vector{T} where T)

Adding parenthesis solves your problem:

(append(a::Vector{T}, b::Vector{T})::Vector{T}) where T = vcat(a, b)

ex2 = Meta.parse("(append(a::Vector{T}, b::Vector{T})::Vector{T}) where T = vcat(a, b)")


julia> ex2.args[1].head

:where

julia> ex2.args[1].args

2-element Vector{Any}:

:(append(a::Vector{T}, b::Vector{T})::Vector{T})

:T

2 Likes