# Why NTuple{2, Ref{Int}} \<: NTuple get false?

**URL:** https://discourse.julialang.org/t/why-ntuple-2-ref-int-ntuple-get-false/69559
**Category:** General Usage
**Tags:** question, ntuple
**Created:** [October 11, 2021, 11:25am UTC](https://discourse.julialang.org/t/why-ntuple-2-ref-int-ntuple-get-false/69559 "2021-10-11T11:25:45Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![zwtechnb](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zwtechnb](https://discourse.julialang.org/u/zwtechnb)
#### Post date: [October 11, 2021, 11:25am UTC](https://discourse.julialang.org/t/why-ntuple-2-ref-int-ntuple-get-false/69559/1 "2021-10-11T11:25:45Z")

</div>

Code:

```julia
julia> NTuple{2, Int} <: NTuple
true

julia> NTuple{2, Ref{Int}} <: NTuple
false

julia> (NTuple{2, T} where T) <: NTuple
true

```

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [October 11, 2021, 11:55am UTC](https://discourse.julialang.org/t/why-ntuple-2-ref-int-ntuple-get-false/69559/2 "2021-10-11T11:55:23Z")

</div>

`Ref` is an abstract type - you’re probably looking for `RefValue`:

```julia
julia> NTuple{2, Base.RefValue{Int}} <: NTuple
true

julia> Ref(1) |> typeof
Base.RefValue{Int64}

```

Alternatively, if you want to be able to have an `NTuple` of any reference type that’s referencing an `Int`, you can use this:

```julia
julia> NTuple{2, <:Ref{Int}} <: NTuple
true

```

This specifies that the elements of the tuple have to be subtypes of `Ref{Int}`.

---

<div class="post-metadata">

### Author: ![zwtechnb](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zwtechnb](https://discourse.julialang.org/u/zwtechnb)
#### Post date: [October 11, 2021, 1:12pm UTC](https://discourse.julialang.org/t/why-ntuple-2-ref-int-ntuple-get-false/69559/3 "2021-10-11T13:12:48Z")

</div>

Same as above question. I am a bit of confusing the following:

```julia
julia> NTuple{2, Integer} <: NTuple{2, T} where T<:Number
false

```

I know Integer is an abstract and Integer \<: Number. What I am reasoning are : the `T` in above code can be any type including abstract type Integer, so above comparing result should be true!

Please help me to understand why result is false!

Thank you!

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [October 11, 2021, 1:15pm UTC](https://discourse.julialang.org/t/why-ntuple-2-ref-int-ntuple-get-false/69559/4 "2021-10-11T13:15:00Z")

</div>

Julia types are invariant, not covariant - see here for more information:

[https://docs.julialang.org/en/v1/manual/types/#man-parametric-composite-types](https://docs.julialang.org/en/v1/manual/types/#man-parametric-composite-types)

---

<div class="post-metadata">

### Author: ![zwtechnb](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zwtechnb](https://discourse.julialang.org/u/zwtechnb)
#### Post date: [October 11, 2021, 1:19pm UTC](https://discourse.julialang.org/t/why-ntuple-2-ref-int-ntuple-get-false/69559/5 "2021-10-11T13:19:12Z")

</div>

Thanks for your comment.

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [October 11, 2021, 6:06pm UTC](https://discourse.julialang.org/t/why-ntuple-2-ref-int-ntuple-get-false/69559/6 "2021-10-11T18:06:51Z")

</div>

> [@Sukera](#):
>
> Julia types are invariant, not covariant - see here for more information:

Tuples are the exception here though. Tuples in Julia are indeed covariant.

The reason for this particular behavior is often referred to as the [diagonal rule](https://docs.julialang.org/en/v1/devdocs/types/#Diagonal-types). It says that for a tuple type to be a subtype of `Tuple{T, T} where T`, `T` always has to be a concrete type. This is the reason why you can write `f(x::T, y::T) where {T} = ...` to dispatch only on the case where `x` and `y` are of the same type.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [October 11, 2021, 7:11pm UTC](https://discourse.julialang.org/t/why-ntuple-2-ref-int-ntuple-get-false/69559/7 "2021-10-11T19:11:56Z")

</div>

> [@simeonschaub](#):
>
> Tuples in Julia are indeed covariant.

I was not aware that this applied to `NTuple` as well, good to know!

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [October 11, 2021, 7:23pm UTC](https://discourse.julialang.org/t/why-ntuple-2-ref-int-ntuple-get-false/69559/8 "2021-10-11T19:23:07Z")

</div>

Yes, `NTuple` is just an alias for `Tuple{Vararg{T, N}} where {N, T}`.
