What does "where T" in julia do without any specification after T?

Viewed 632

I'm new to julia and I have not been able to find an explanation of where that fully makes sense to me. So I understand that for example function f(x::T, y::T) where {T<:Number} = true is requiring that both the x and y parameters be numeric types of some sort.

But then there are other functions that will look like function f(X::AbstractMatrix{T}) where T without any specification of what T is supposed to be. Can someone explain what that is doing and when I would want to use this syntax?

2 Answers

where T without anything else is just where {T<:Any} in other words, it's true for all T, but Julia needs you to write something so that `T is defined.

Oscar's answer is correct, but to answer your second question,

When I would want to use this syntax?

like mbauman suggested in the comments, a typical use for this syntax is when you want to use the type in the body of the function. Maybe the example in the docs helps:

julia> mytypeof(x::T) where {T} = T
mytypeof (generic function with 1 method)

julia> mytypeof(1)
Int64

julia> mytypeof(1.0)
Float64
Related