Meaning of {T}

More concretely, the stuff that appears within those {} brackets in function definition syntax f{…}(…) define the names that are used as TypeVars in the argument list that follows. TypeVars are kinda like wildcards; they try to match against the arguments that you pass. Those names are then bound to what they match — and you can use them within the function body. You can also restrict the TypeVars to be subtypes of an abstract type within those curly brackets.

julia> f{T}(A::Vector{T}) = "eltype: $T"
       f{T<:Number}(A::Vector{T}) = "number eltype: $T"
f (generic function with 2 methods)

julia> f([:a, :b, :c])
"eltype: Symbol"

julia> f([1,2,3])
"number eltype: Int64"

While TypeVars are often used in conjunction with parametric types (like how I used Vector{T} above), the curly brackets in those two contexts have very different meanings. The syntax Vector{T} needs T to already be bound — either to a TypeVar that was introduced in a function definition or bound directly to a type.

7 Likes