Odd syntax error with type parameter and array literal

I recently ran into a weird parsing error with Julia when defining the following function:

julia> function to_array(val::T)::Array{T} where {T} [val] end
ERROR: syntax: space before "[" not allowed in "{T} [" at REPL[14]:1
Stacktrace:
 [1] top-level scope at none:1

If I add a newline before the function body, it parses and runs fine.

If I remove the type parameter, I get the same syntax error but with the return type:

julia> function to_arr(val::T)::Array{T} [val] end
ERROR: syntax: space before "[" not allowed in "Array{T} [" at REPL[27]:1
Stacktrace:
 [1] top-level scope at none:1

Does anybody have any idea why this happens? I don’t know of any language feature that allows square brackets in this position. I guess some parsing step is failing and the parser doesn’t know to backtrack and attempt to parse the block, but I don’t really know anything about how the parser works so I can’t really speculate.

2 Likes

That’s looks like a bug.
Open an issue at Issues · JuliaLang/julia · GitHub

1 Like