# Tuple Type with unordered parameters

**URL:** <https://discourse.julialang.org/t/tuple-type-with-unordered-parameters/23427>\
**Category:** General Usage\
**Created:** [April 23, 2019, 3:50am UTC](https://discourse.julialang.org/t/tuple-type-with-unordered-parameters/23427 "2019-04-23T03:50:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![neyhay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neyhay/32/5098_2.png) [@neyhay](https://discourse.julialang.org/u/neyhay)\
**Post date:** [April 23, 2019, 3:50am UTC](https://discourse.julialang.org/t/tuple-type-with-unordered-parameters/23427/1 "2019-04-23T03:50:38Z")

</div>

Is it possible to define a Tuple Type based _only_ on the parameters of the Tuple and not the order? For example: `Tuple{Float64, Char} == Tuple{Char, Float64}` evaluates false, but I would like to create the possibility that these are identical.

For background, I have a number of Composite Types, `MyTypeA, MyTypeB, ...` , and I would like to define a Tuple Type that refers to specific combinations of Types:

```julia
MyCombinationA = Tuple{MyTypeA,MyTypeB,MyTypeC}
MyCombinationB = Tuple{MyTypeA,MyTypeD,MyTypeE,MyTypeF}
...

```

Ideally I could define the combination once and avoid having to write out the definition of every single permutation.

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [April 23, 2019, 3:53am UTC](https://discourse.julialang.org/t/tuple-type-with-unordered-parameters/23427/2 "2019-04-23T03:53:38Z")

</div>

Order is pretty fundamental to the definition of `Tuple`, so there’s not a way to ignore order. Maybe you could use a `Union{MyTypeA, MyTypeB, MyTypeC}` somehow, since order _isn’t_ significant for Union 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:** [April 23, 2019, 7:36am UTC](https://discourse.julialang.org/t/tuple-type-with-unordered-parameters/23427/3 "2019-04-23T07:36:56Z")

</div>

You could define a wrapper type an have an (inner) constructor that imposes a unique order for the type parameter.

---

<div class="post-metadata">

**Author:** ![neyhay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neyhay/32/5098_2.png) [@neyhay](https://discourse.julialang.org/u/neyhay)\
**Post date:** [April 25, 2019, 4:57am UTC](https://discourse.julialang.org/t/tuple-type-with-unordered-parameters/23427/5 "2019-04-25T04:57:20Z")

</div>

Thanks for your suggestions. I tried `Union{}` and the only way I get the the specificity I’m looking for is like this:

```julia
CombA = Union{Tuple{TypeA,TypeB,TypeC},Tuple{TypeA,TypeC,TypeB},
              Tuple{TypeB,TypeA,TypeC},Tuple{TypeB,TypeC,TypeA},
              Tuple{TypeC,TypeA,TypeB},Tuple{TypeC,TypeB,TypeA}}

```

which gets obnoxious quickly.  
If I impose a unique order instead, how can I get julia to sort them? The docs ([Essentials · The Julia Language](https://docs.julialang.org/en/v1.0/base/base/#Base.isless)) suggest I should implement `isless()`. How do I go about doing that?

---

<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:** [April 25, 2019, 5:03am UTC](https://discourse.julialang.org/t/tuple-type-with-unordered-parameters/23427/6 "2019-04-25T05:03:59Z")

</div>

Something like

```julia
sort_Tuple(::Type{T}) where {T <: Tuple} =
    Tuple{sort(collect(fieldtypes(T)); by = String)...}

```

eg

```julia
julia> sort_Tuple(Tuple{Int,Float64})
Tuple{Float64,Int64}

```

---

<div class="post-metadata">

**Author:** ![neyhay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neyhay/32/5098_2.png) [@neyhay](https://discourse.julialang.org/u/neyhay)\
**Post date:** [April 25, 2019, 6:16pm UTC](https://discourse.julialang.org/t/tuple-type-with-unordered-parameters/23427/7 "2019-04-25T18:16:19Z")

</div>

This is quite nice, thanks. I’ll go this route then.  
I had to change `by=String` → `by=string` otherwise I would get an error.
