By the way, you can actually do this with local bindings[1]
julia> let a::Vector{Float32} = [1,2,3,4]
a
end
4-element Vector{Float32}:
1.0
2.0
3.0
4.0
Though, be aware that this allocates two arrays, one that is [1,2,3,4] and a new one from the implicit call to convert(Vector{Float32}, [1,2,3,4]). One day Julia May be able to optimize away that allocation, but currently it cannot.
Though I guess you could instead write
julia> let a::Vector{Float32} = 1:4
a
end
4-element Vector{Float32}:
1.0
2.0
3.0
4.0
[1] in version 1.8, this will be possible even in the global scope
makes an array with elements of type T. It is better than T.([1,2,3]), because the latter will first make a Vector{Int}, and then convert it to a Vector{T}, while T[1,2,3] will directly create the wanted array type, no extra allocation.
This doesn’t work since func isn’t a type (or, technically, because it has no getindex method.)
Thanks! That clearly shows that I should search the official Julia documentation rather than Google. A Google search for “Julia array literal” didn’t (quickly enough) lead me to this answer.
Reading the other answers (Thank you all!), I now understand that T[1,2,3,4] really is the syntax for array literal.
I’ve also learned that the type annotation works only on variables, not on literals.