# What is difference between Type{T} and T

**URL:** <https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325>\
**Category:** New to Julia\
**Tags:** type, parametric-types\
**Created:** [January 6, 2019, 3:42pm UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325 "2019-01-06T15:42:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![vickysharma0812](https://avatars.discourse-cdn.com/v4/letter/v/f0a364/32.png) [@vickysharma0812](https://discourse.julialang.org/u/vickysharma0812)\
**Post date:** [January 6, 2019, 3:42pm UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/1 "2019-01-06T15:42:15Z")

</div>

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

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

```

why cannot we use

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

```

please excuse me if this question is too basic.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 6, 2019, 3:53pm UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/2 "2019-01-06T15:53:39Z")

</div>

did you read the documentation?

[https://docs.julialang.org/en/v1/manual/types/#man-singleton-types-1](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](https://docs.julialang.org/en/v1/manual/methods/#Extracting-the-type-parameter-from-a-super-type-1)

---

<div class="post-metadata">

**Author:** ![Non-Contradiction](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-contradiction/32/2208_2.png) [@Non-Contradiction](https://discourse.julialang.org/u/Non-Contradiction)\
**Post date:** [January 6, 2019, 4:12pm UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/3 "2019-01-06T16:12:33Z")

</div>

The documentation of `convert` is

```julia
convert(T, x)

  Convert x to a value of type T.

```

And an example is

```julia
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.

---

<div class="post-metadata">

**Author:** ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)\
**Post date:** [January 6, 2019, 4:18pm UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/4 "2019-01-06T16:18:17Z")

</div>

`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.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 6, 2019, 6:32pm UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/5 "2019-01-06T18:32:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vickysharma0812](https://avatars.discourse-cdn.com/v4/letter/v/f0a364/32.png) [@vickysharma0812](https://discourse.julialang.org/u/vickysharma0812)\
**Post date:** [January 6, 2019, 6:33pm UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/6 "2019-01-06T18:33:14Z")

</div>

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

regards  
Vikas

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [January 6, 2019, 7:45pm UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/7 "2019-01-06T19:45:24Z")

</div>

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

```julia
DataType <-------- Float64
Type{Float64} <-/

```

```julia
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”

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [January 6, 2019, 9:05pm UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/8 "2019-01-06T21:05:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![path-doc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/path-doc/32/19183_2.png) [@path-doc](https://discourse.julialang.org/u/path-doc)\
**Post date:** [March 10, 2021, 2:33pm UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/9 "2021-03-10T14:33:27Z")

</div>

A quick question: is `DataType` the root of the “type tree”?

And one longer one: how/where is this tree defined?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 10, 2021, 4:12pm UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/10 "2021-03-10T16:12:12Z")

</div>

Think of [`DataType`](https://docs.julialang.org/en/v1.7-dev/manual/types/#man-declared-types) as the representation of types. I am not sure would consider the latter a tree in any sense; perhaps clarify what you mean.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [March 10, 2021, 4:27pm UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/11 "2021-03-10T16:27:27Z")

</div>

> [@path-doc](#):
>
> is `DataType` the root of the “type tree”?

No, `Any` is the root of the type tree. Putting aside that I see `DataType` as kind of a “meta type”:

```julia
julia> supertype(DataType)
Type{T}

julia> supertype(Type{T} where T)
Any

julia> supertype(Any)
Any

```

or with a small helper function:

```julia
julia> show_supertypes(T) = print(join(supertypes(T), " <: "))
show_supertypes (generic function with 1 method)

julia> show_supertypes(DataType)
DataType <: Type{T} <: Any

```

On a side note, if you want to see a branch of the type tree you can do

```julia
using AbstractTrees
AbstractTrees.children(x) = subtypes(x)
print_tree(Number)

```

to get

```julia
julia> print_tree(Number)
Number
├─ Complex
└─ Real
   ├─ AbstractFloat
   │ ├─ BigFloat
   │ ├─ Float16
   │ ├─ Float32
   │ └─ Float64
   ├─ AbstractIrrational
   │ └─ Irrational
   ├─ Integer
   │ ├─ Bool
   │ ├─ Signed
   │ │ ├─ BigInt
   │ │ ├─ Int128
   │ │ ├─ Int16
   │ │ ├─ Int32
   │ │ ├─ Int64
   │ │ └─ Int8
   │ └─ Unsigned
   │ ├─ UInt128
   │ ├─ UInt16
   │ ├─ UInt32
   │ ├─ UInt64
   │ └─ UInt8
   └─ Rational

```

---

<div class="post-metadata">

**Author:** ![path-doc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/path-doc/32/19183_2.png) [@path-doc](https://discourse.julialang.org/u/path-doc)\
**Post date:** [March 11, 2021, 1:34am UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/12 "2021-03-11T01:34:21Z")

</div>

@carstenbauer, I guess you know this, but according to `print_tree`, `DataType` is a leaf on the “tree”. (It is the unique element on the branch of subtypes.)

However, according to `isa` (mentioned in @baggepinnen’s post), all the following evaluate to true:

- `isa(Type, Any)`
- `isa(Any, Type)`
- `isa(DataType, Any)`
- `isa(Any, DataType)`.

But `DataType` is not really at the top/bottom of the order because, in addition, we have

- `isa(Type, DataType)` is false
- `isa(DataType, Type)` is true.

But also note that this violation of transitivity (`DataType <: Type ~: Any ~: DataType`) implies that, once we include Singleton Types, we do not have a preorder let alone a partial order or a tree.

I might try and create a graph on the basis of `isa` to get a lovely diagram like yours as a way of honing my Julia skills. At the moment, I can’t see a completely obvious way on the basis of the existing functions in `AbstractTrees`.

PS. Just out of interest, we also have

- `isa(typeof(Type), DataType)` is true
- `isa(DataType, typeof(Type))` is false  
and
- `isa(typeof(Type{T} where T), DataType)` is true
- `isa(DataType, typeof(Type{T} where T))` is false.

---

<div class="post-metadata">

**Author:** ![path-doc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/path-doc/32/19183_2.png) [@path-doc](https://discourse.julialang.org/u/path-doc)\
**Post date:** [March 11, 2021, 2:11pm UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/13 "2021-03-11T14:11:26Z")

</div>

PPS. How come your types are all blue and mine are not? 🙂

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [March 11, 2021, 2:29pm UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/14 "2021-03-11T14:29:21Z")

</div>

Hm? What do you mean?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 11, 2021, 2:40pm UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/15 "2021-03-11T14:40:41Z")

</div>

He probably wrote:

```julia  
code here  
```

For some minimal highlight support.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 11, 2021, 2:50pm UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/16 "2021-03-11T14:50:07Z")

</div>

I think you are confusing things. `isa` shows if the **type of the first argument** `<: the second argument`. So does not make sense to me to use `isa` to establish any ordering/tree, you should be using `<:` for it.

```julia
julia> Any <: Type
false

julia> Type <: Any
true

julia> Any <: DataType
false

julia> DataType <: Any
true

```

You should not use `isa` passing a type as the first argument, because it will always be the same as `DataType <: second_isa_argument` because the type of any type will be `DataType`.

---

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [March 11, 2021, 3:11pm UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/17 "2021-03-11T15:11:01Z")

</div>

> [@Henrique\_Becker](#):
>
> You should not use `isa` passing a type as the first argument, because it will always be the same as `DataType <: second_isa_argument` because the type of any type will be `DataType` .

That’s not true:

```julia
julia> Int64 isa Type{Int64}
true

julia> DataType <: Type{Int64}
false

```

I can’t find it, but there is a recording of a talk, probably given by Jeff, that shows a 2D grid layout of values, with one dimension showing a subtyping relation and another dimension showing the `typeof` relation. It shows the following self-loops:

```julia
julia> typeof(DataType)
DataType

julia> supertype(Any)
Any

```

If someone can find that talk, I would appreciate a time-link to it and a screenshot of that slide.

---

<div class="post-metadata">

**Author:** ![path-doc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/path-doc/32/19183_2.png) [@path-doc](https://discourse.julialang.org/u/path-doc)\
**Post date:** [March 12, 2021, 12:08am UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/18 "2021-03-12T00:08:43Z")

</div>

> [@Henrique\_Becker](#):
>
> `isa` shows if the **type of the first argument** `<: the second argument` .

Let’s agree that because

```julia
julia> isa(Any, DataType)
true

```

and

```julia
julia> isa(Any, Any)
true

```

we can safely say that `Any` is both of type `Any` and of type `DataType`. The whole point of type hierarchies is to be able to think of “meta types” as being inside the system. Otherwise we are artificially restricting ourselves to reasoning/operating at a lower level.

Let’s also agree that `isa` and `<:` can be described as two distinct binary relations, both of which describe relationships among types. You will note that on [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/), there are certain more abstract types where `isa` is used instead of `<:` . I expect that `<:` does not account for relationships higher-up the type hierarchy.

I guess the first natural question to ask is whether `<:` is a partial order.

Also, although my arguments in the previous post show that `isa` is not a preorder. They do not exclude the possibility that `<:` is a subrelation of `isa`, where some symmetry relationships under `isa` are broken by `<:` .

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 12, 2021, 12:45am UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/19 "2021-03-12T00:45:09Z")

</div>

> [@goretkin](#):
>
> That’s not true:
> 
> ```julia
> julia> Int64 isa Type{Int64}
> true
> 
> julia> DataType <: Type{Int64}
> false
> 
> ```

You are correct, I forgot this exception. However, `Type{T} where {T}` is kinda of a special object, it represents the singleton type for which the only instance is `T` (this is literally the documentation of `Core.Type`). I believe we cannot have `typeof(Int64)` as `Type{Int64}` because then `typeof(typeof(Int64))` would be `Type{Type{Int64}}` and so on, instead of the cycle at `DataType` but I am not sure what would break by doing this.

> [@path-doc](#):
>
> Then why do we have
> 
> ```julia
> julia> isa(Any, DataType)
> true
> 
> ```
> 
> and
> 
> ```julia
> julia> Any <: DataType
> false
> 
> ```

If you tried to follow what I meant by my statement then you missed it:

```julia
julia> isa(Any, DataType)
true

julia> typeof(Any) <: DataType
true

```

---

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [March 12, 2021, 1:11am UTC](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325/20 "2021-03-12T01:11:33Z")

</div>

> [@Henrique\_Becker](#):
>
> However, `Type{T} where {T}` is kinda of a special object, it represents the singleton type for which the only instance is `T` (this is literally the documentation of `Core.Type` ).

As far as I understand it, that is correct, but there’s certainly reason to doubt myself and be confused about it:

> <https://github.com/JuliaLang/julia/pull/36591#issuecomment-655931353>
>
> \> is there a terminology for \`Type{T}\` now that we don't confuse it with singlet…on types? If not, do we want to introduce one, eg call it "type selectors"?
> 
> Yeah, calling \`Type{T}\` a singleton sounds a bit confusing since you can write something like \`Type{\<:Integer}\`. For the new name, "type selectors" sound reasonable to me.
> 
> \> should I expunge the remaining use of 'singleton' for \`Type{T}\` from \_Conversion and Promotion\_?
> 
> :+1: from me :)

> <https://github.com/JuliaLang/julia/issues/38716#issuecomment-739257832>
>
> For one use case, I am thinking of \`Int64\` and \`round\` as functions:
> 
> \`\`\`juli…a
> julia\> Int64(3)
> 3
> 
> julia\> round(3)
> 3
> \`\`\`
> 
> and I want to represent their types respectively. Unfortunately
> 
> \`\`\`julia
> julia\> isbitstype(typeof(round))
> true
> 
> julia\> isbitstype(Type{Int64})
> false
> \`\`\`
> 
> And more pertinent to my problem:
> 
> \`\`\`julia
> julia\> struct Foo{T}
> \_::T
> end
> 
> julia\> Foo(round)
> Foo{typeof(round)}(round)
> 
> julia\> sizeof(Foo(round))
> 0
> 
> julia\> Foo(Int64)
> Foo{DataType}(Int64)
> 
> julia\> sizeof(Foo(Int64))
> 8
> 
> julia\> Foo{Type{Int64}}(Int64)
> Foo{Type{Int64}}(Int64)
> 
> julia\> sizeof(Foo{Type{Int64}}(Int64))
> 8
> \`\`\`
> 
> If I understand correctly, if \`T\` in \`Foo{T}\` is a singleton type, that is there is only one value \`x::T\`, then \`sizeof(Foo{T})\` can in principle be \`0\`. There is only one \`x::typeof(round)\` and only one \`x::Type{Int64}\`.
> 
> (reference: https://julialang.zulipchat.com/#narrow/stream/225542-helpdesk/topic/size.20of.20tuple.20.2F.20struct.20of.20singleton.20type)

[Next page](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325.md?page=2)
