What is difference between Type{T} and T

type
parametric-types

#1

Hello everyone,
I have just started to learn about Julia(<:1.0).

Unfortunately, I am not able to understand the use/functionality of Type{T}, for example, in

convert(::Type{T}, a::T) where {T<:AbstractArray} = a

why cannot we use

convert(::T, a::T) where {T<:AbstractArray} = a

please excuse me if this question is too basic.


#2

did you read the documentation?

https://docs.julialang.org/en/v1/manual/types/#man-singleton-types-1

also, for an example,

https://docs.julialang.org/en/v1/manual/methods/#Extracting-the-type-parameter-from-a-super-type-1


#3

The documentation of convert is

convert(T, x)

  Convert x to a value of type T.

And an example is

julia> convert(Int, 3.0)
  3

So you can see the first argument is a Type, so its type is Type{T}, and the second argument is an object to be converted to that type.


#4

foo(::T, a::S) where {T,S} - the first argument of foo is a (non-usable) variable of type T, the second, namely a of type S.

foo(::Type{T}, a::S)- the first argument of foo is the type T, the second is of type S.

So, the difference between ::T and ::Type{T} is that they request either a variable of type T or a variable which is the type T itself. In julia types are data as well.

Indeed, this is a very basic question that is easily addressed by reading the documentation and actually running the code in question and seeing its effects.


#5

Please everyone, keep in mind that this question was appropriately posted in the usage / first steps category. Even if something is documented, it’s still not a problem to ask a question about it! Hopefully the answers here help clarify things for @vickysharma0812.


#6

Thank you for reply,
I have understood the usage and importance of Type{T}.

regards
Vikas


#7

Type{T} is admittedly slightly unintuitive as it provides an unexpected branch in the type tree

DataType <-------- Float64
Type{Float64} <-/
julia> typeof(Float64)
DataType

julia> Float64 isa DataType
true

julia> Float64 isa Type{Float64} # This in comibnation with the next line might be unexpected
true

julia> DataType isa Type{Float64} 
false

which the documentation hints at by calling it “special”


#8

Just to add on, often, when you are new to something, even if you read the documentation not everything clicks. More often than not you are unsure why some passage or sentence matters until you’ve understood the concepts. This is why it’s helpful to point to the documentation and explain briefly the concepts as well.