# Type with arbitrary number of parameters

**URL:** <https://discourse.julialang.org/t/type-with-arbitrary-number-of-parameters/99374>\
**Category:** New to Julia\
**Tags:** parametric-types\
**Created:** [May 25, 2023, 11:16am UTC](https://discourse.julialang.org/t/type-with-arbitrary-number-of-parameters/99374 "2023-05-25T11:16:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [May 25, 2023, 11:16am UTC](https://discourse.julialang.org/t/type-with-arbitrary-number-of-parameters/99374/1 "2023-05-25T11:16:17Z")

</div>

Hello!

I am struggling to create a type with an arbitrary number of parameters, similarly to what `Tuple` does.

Ideally, I would use it in functions like this:

```julia
julia> function f(type_list, string_list)
    map(zip(type_list, string_list)) do (t, s)
        parse(t,s)
    end
end
julia> f([Int,Float64], ["1","3"])
2-element Vector{Real}:
 1
 3.0

```

That type list could ideally come from the type, so that I could do something like:

```julia
function f(type_with_parameters{T...}, string_list)
    map(zip(T, string_list)) do (t, s)
        parse(t,s)
    end
end

```

Thank you in advance!

---

<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:** [May 25, 2023, 11:20am UTC](https://discourse.julialang.org/t/type-with-arbitrary-number-of-parameters/99374/2 "2023-05-25T11:20:48Z")

</div>

`Tuple` is a special builtin; it’s ability to have a variable number of type parameters is not something you can replicate in a new type. You can however use a tuple directly for what you’re doing.

---

<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:** [May 25, 2023, 2:12pm UTC](https://discourse.julialang.org/t/type-with-arbitrary-number-of-parameters/99374/3 "2023-05-25T14:12:41Z")

</div>

Or have the only type parameter to be a tuple.

---

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [May 25, 2023, 2:16pm UTC](https://discourse.julialang.org/t/type-with-arbitrary-number-of-parameters/99374/4 "2023-05-25T14:16:58Z")

</div>

Yeah, I thought so. However, I don’t see how I can do that:

```julia

julia> struct MyType{T<:Tuple}
       end

julia> MyType{(Int,Float64)}
ERROR: TypeError: in Type, in parameter, expected Type, got a value of type Tuple{DataType, DataType}
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

julia> struct MyType2{T} where T <: Tuple
       end
ERROR: syntax: invalid type signature around REPL[4]:1
Stacktrace:
 [1] top-level scope
   @ REPL[4]:1

julia> struct MyType2{T} where {T <: Tuple}
       end
ERROR: syntax: invalid type signature around REPL[5]:1
Stacktrace:
 [1] top-level scope
   @ REPL[5]:1

julia> struct MyType2{T::Tuple}
       end
ERROR: syntax: invalid variable expression in "where" around REPL[6]:1
Stacktrace:
 [1] top-level scope
   @ REPL[6]:1

julia> struct MyType2{::Tuple}
       end
ERROR: syntax: invalid variable expression in "where" around REPL[7]:1
Stacktrace:
 [1] top-level scope
   @ REPL[7]:1

```

---

<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:** [May 25, 2023, 5:31pm UTC](https://discourse.julialang.org/t/type-with-arbitrary-number-of-parameters/99374/5 "2023-05-25T17:31:32Z")

</div>

```julia
julia> struct MyType{T<:Tuple}
       end

julia> MyType{Tuple{Int,Float64}}
MyType{Tuple{Int64, Float64}}

```

`(Int,Float64)` is not a type, it is a tuple of two types. `Tuple{Int64, Float64}` is a type.

---

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [May 25, 2023, 5:55pm UTC](https://discourse.julialang.org/t/type-with-arbitrary-number-of-parameters/99374/6 "2023-05-25T17:55:40Z")

</div>

Great, thanks!

As a side note, since last Julia version that tuples have wider support as parameters for types, shouldn’t my version of it (using tuples instead of the type tuple with parameters) work?

---

<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:** [May 25, 2023, 9:09pm UTC](https://discourse.julialang.org/t/type-with-arbitrary-number-of-parameters/99374/7 "2023-05-25T21:09:45Z")

</div>

I have no knowledge about that. Can you point me to the documentation of this change?

---

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [May 25, 2023, 10:14pm UTC](https://discourse.julialang.org/t/type-with-arbitrary-number-of-parameters/99374/8 "2023-05-25T22:14:20Z")

</div>

Not really applicable to types, but for is it’s values, but I meant to say that the same could be applied to types.

Anyway: [allow nested combinations of (named)tuples, symbols, and bits as type parameters by JeffBezanson · Pull Request #46300 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/46300)

---

<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:** [May 25, 2023, 11:00pm UTC](https://discourse.julialang.org/t/type-with-arbitrary-number-of-parameters/99374/9 "2023-05-25T23:00:16Z")

</div>

> [@nandoconde](#):
>
> As a side note, since last Julia version that tuples have wider support as parameters for types, shouldn’t my version of it (using tuples instead of the type tuple with parameters) work?

No, because `T <: Tuple` means “a type `T` subtyping `Tuple`”, not “an object whose type is a subtype of `Tuple`”. Your restriction requires a type - you cannot express the constraint “an object that is of a specific type” in a type parameter.

The important distinction is that an object and its type are not the same thing.

---

<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:** [May 26, 2023, 12:18am UTC](https://discourse.julialang.org/t/type-with-arbitrary-number-of-parameters/99374/10 "2023-05-26T00:18:51Z")

</div>

This change only applies for (named) tuples that have inside `isbits` objects and/or `Symbols` and/or (named) tuples following this same restriction. Your `(Int,Float64)` does not match these per-requisites:

```julia
julia> isbits(Int)
false

julia> isbits(Float64)
false

julia> isbits(1)
true

julia> isbits(1.0)
true

```

A tuple `(1, 1.0)` could would match them, but it is an entirely different thing.

---

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [May 26, 2023, 5:30am UTC](https://discourse.julialang.org/t/type-with-arbitrary-number-of-parameters/99374/11 "2023-05-26T05:30:29Z")

</div>

Yeah, I figured. Thanks!!
