# Instantiation of Tuple with value type parameters

**URL:** https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673
**Category:** General Usage
**Created:** [July 7, 2020, 12:12pm UTC](https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673 "2020-07-07T12:12:14Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![vlc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vlc/32/4705_2.png) [@vlc](https://discourse.julialang.org/u/vlc)
#### Post date: [July 7, 2020, 12:12pm UTC](https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673/1 "2020-07-07T12:12:14Z")

</div>

Hi there,

I would like to be able to instantiate `Tuple`s with value type parameters, _e.g._

```julia
foo = Tuple{3,4}()

```

that is, similarly to how `Val` does it

```julia
bar = Val(3)

```

or

```julia
baz = Val{3}()

```

I was wondering whether there was way to achieve this.

Any pointers would be much appreciated! Thanks!

---

<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: [July 7, 2020, 12:22pm UTC](https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673/2 "2020-07-07T12:22:15Z")

</div>

I am not sure I understand what you are trying to do, but `3,4` are not valid type parameters for `Tuple` — you need _types_. So as written, the above is impossible.

`Tuple{Val{3},Val{4}}` would be a valid _type_, but there are no general constructors to give you an _instance_ of a `Tuple`.

---

<div class="post-metadata">

### Author: ![vlc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vlc/32/4705_2.png) [@vlc](https://discourse.julialang.org/u/vlc)
#### Post date: [July 7, 2020, 12:29pm UTC](https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673/3 "2020-07-07T12:29:50Z")

</div>

Hi Tamas,

First of, thanks for looking into this. Here is another way to put it… On the one hand, I can do define a type like this

```julia
struct Foo{T} end

```

and instantiate it with `Tuple{3,4}` as type parameter

```julia
Foo{Tuple{3,4}}()

```

and this works fine.

On the other hand, and for what I’m trying to do, it would be more convenient to have the type `Foo` to have a field of type `T`, that is

```julia
struct Foo{T}
    x::T
end

```

but if I can not create instantiate objects of type `Tuple{3,4}` this won’t work…

Please let me know if that still does not make sense… Thanks again!

---

<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: [July 7, 2020, 12:52pm UTC](https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673/4 "2020-07-07T12:52:44Z")

</div>

This is not possible, as there are no values which have type `Tuple{3,4}`.

I am still not sure I understand the context though. How is a parametric type which is technically invalid useful for you?

---

<div class="post-metadata">

### Author: ![vlc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vlc/32/4705_2.png) [@vlc](https://discourse.julialang.org/u/vlc)
#### Post date: [July 7, 2020, 12:57pm UTC](https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673/5 "2020-07-07T12:57:56Z")

</div>

Is there a reason why that’s not possible (_i.e._ to create values with `Tuple{3,4}` type)?

The use is similar to how `StaticArrays` passes `Tuple{3,4}` as size parameters, with “size” info sometimes statically-typed if I want it,

```julia
sfoo = Foo(Tuple{3,4}())

```

or not

```julia
sfoo = Foo((3,4))

```

---

<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: [July 7, 2020, 1:02pm UTC](https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673/6 "2020-07-07T13:02:02Z")

</div>

> [@vlc](#):
>
> Is there a reason why that’s not possible ( _i.e._ to create values with `Tuple{3,4}` type)?

Yes — there is no _value_ that has _type_ `3`. Eg

```julia
julia> (1,2) isa Tuple{Int,Int} 
true

```

but for what `x` would `x isa Tuple{3,4}` hold?

> [@vlc](#):
>
> similar to how `StaticArrays` passes `Tuple{3,4}` as size parameters

You can use type parameters like this, but no fields can have this type.

---

<div class="post-metadata">

### Author: ![vlc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vlc/32/4705_2.png) [@vlc](https://discourse.julialang.org/u/vlc)
#### Post date: [July 7, 2020, 1:05pm UTC](https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673/7 "2020-07-07T13:05:23Z")

</div>

I guess I don’t see why this

```julia
julia> Val(3) isa Val{3}
true

```

would be possible, but not

```julia
julia> Tuple{3,4}() isa Tuple{3,4}

```

_e.g._ to hold more than one value with wrapping the whole thing in a tuple.

Is there a fundamental difference, or is it technically not possible?

Thanks again!

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [July 7, 2020, 1:15pm UTC](https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673/8 "2020-07-07T13:15:49Z")

</div>

> [@vlc](#):
>
> hold more than one value with wrapping the whole thing in a tuple.

You did not wrap anything in a tuple. That is done by `Val{(3,4)}` and that’s totally fine.

The two has nothing to do with each other. You are trying to construct an instance so you need to figure out what you are trying to construct. The type parameters in each type have their specific meaning. For `Val` it’s unused and for `Tuple` it has to be types.

---

<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: [July 7, 2020, 1:16pm UTC](https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673/9 "2020-07-07T13:16:57Z")

</div>

I think you may be confused because `Val(x)` is a shorthand constructor for `Val{x}()`, which is itself a constructor for a [singleton type](https://docs.julialang.org/en/v1/manual/types/#man-singleton-types-1).

You could define something similar as

```julia
struct Foo{T} end
@inline Foo(T) = Foo{T}()

```

if that’s what you want.

---

<div class="post-metadata">

### Author: ![vlc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vlc/32/4705_2.png) [@vlc](https://discourse.julialang.org/u/vlc)
#### Post date: [July 7, 2020, 5:54pm UTC](https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673/10 "2020-07-07T17:54:41Z")

</div>

The way I see it, `Tuple{3,4}` is also a singleton type, and therefore I expected

```julia
julia> x = Tuple{3,4}()

```

to just work. If it were possible, then any number of parameters could be passed without needed to be wrapped as in @yuyichao’s suggestion (`Val((3,4))`), which I thought would be pretty neat.

---

<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: [July 7, 2020, 6:07pm UTC](https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673/11 "2020-07-07T18:07:05Z")

</div>

Again,

> [@Tamas\_Papp](#):
>
> for what `x` would `x isa Tuple{3,4}` hold?

---

<div class="post-metadata">

### Author: ![vlc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vlc/32/4705_2.png) [@vlc](https://discourse.julialang.org/u/vlc)
#### Post date: [July 7, 2020, 6:13pm UTC](https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673/12 "2020-07-07T18:13:06Z")

</div>

For none if no instance of the singleton type `Tuple{3,4}` can’t be created…

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [July 7, 2020, 6:22pm UTC](https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673/13 "2020-07-07T18:22:03Z")

</div>

> [@vlc](#):
>
> The way I see it, `Tuple{3,4}` is also a singleton type, and therefore I expected

Well, that’s not the definition of a singleton type in julia. A singleton type in julia is a type that has only one instance (i.e. no field). Based on the definition of the `Tuple` type, a `Tuple{3,4}` is a type with a field of value `3` and a field of value `4`. That’s just invalid.

Of course you may say that field of invalid type is not a field but making this kind of argument for corner cases isn’t what a language should do. There’s simply no reason to allow it. In fact, even `Tuple{3,4}` should have been an error since it’s not really a valid type. It is only allowed because some package was using it before people realized.

> [@vlc](#):
>
> For none if no instance of the singleton type `Tuple{3,4}` can’t be created…

And there, that conflicts with the definition of singleton type.

---

<div class="post-metadata">

### Author: ![vlc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vlc/32/4705_2.png) [@vlc](https://discourse.julialang.org/u/vlc)
#### Post date: [July 7, 2020, 7:24pm UTC](https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673/14 "2020-07-07T19:24:10Z")

</div>

Ok that is starting to makes sense to me, thanks.

So if I understand correctly, the `Tuple{Val{3},Val{4}}` type is valid julia, just like `Val{(3,4)}` whereas `Tuple{3,4}` is tolerated for backward compatibility issues?

Thanks again!

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [July 7, 2020, 7:41pm UTC](https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673/15 "2020-07-07T19:41:40Z")

</div>

Yes.

Backward compatibility and also the fact that this is the only way to put variable number of type parameters in so the use for it might not have been easily replacable. (I didn’t read the discussion too carefully when it came up a while ago)

---

<div class="post-metadata">

### Author: ![vlc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vlc/32/4705_2.png) [@vlc](https://discourse.julialang.org/u/vlc)
#### Post date: [July 7, 2020, 8:05pm UTC](https://discourse.julialang.org/t/instantiation-of-tuple-with-value-type-parameters/42673/16 "2020-07-07T20:05:03Z")

</div>

Got it, thanks a lot!
