# Syntactic sugar for dispatch on a single type parameter

**URL:** <https://discourse.julialang.org/t/syntactic-sugar-for-dispatch-on-a-single-type-parameter/42719>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 8, 2020, 10:46am UTC](https://discourse.julialang.org/t/syntactic-sugar-for-dispatch-on-a-single-type-parameter/42719 "2020-07-08T10:46:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![michakraus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michakraus/32/222105_2.png) [@michakraus](https://discourse.julialang.org/u/michakraus)\
**Post date:** [July 8, 2020, 10:46am UTC](https://discourse.julialang.org/t/syntactic-sugar-for-dispatch-on-a-single-type-parameter/42719/1 "2020-07-08T10:46:04Z")

</div>

I have some code using traits, where I want to dispatch on a specific type parameter of a function argument. Is there a shortcut for omitting the other parameters, if the one I want to dispatch on is not the first parameter?

For definiteness, consider an example of a type with three parameters:

```julia
struct mystruct{T1,T2,T3}
end

```

If I want to dispatch on the last parameter I have to write either something like

```julia
test(::mystruct{<:Any,<:Any,<:Nothing}) = true
test(::mystruct{<:Any,<:Any,<:Any}) = false

```

or

```julia
test(::mystruct{T1,T2,T3}) where {T1,T2,T3<:Nothing} = true
test(::mystruct{T1,T2,T3}) where {T1,T2,T3} = false

```

In the actual code there are more than just three type parameters, hence it would be convenient not having to spell out all parameters, e.g., something like

```julia
test(::mystruct{,,<:Nothing}) = true
test(::mystruct{,,<:Any}) = false

```

---

<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 8, 2020, 10:51am UTC](https://discourse.julialang.org/t/syntactic-sugar-for-dispatch-on-a-single-type-parameter/42719/2 "2020-07-08T10:51:00Z")

</div>

> [@michakraus](#):
>
> Is there a shortcut for omitting the other parameters, if the one I want to dispatch on is not the first parameter?

Sort of — define an alias that changes the order. Eg

```julia
struct Foo{A,B} end
const Bar{B,A} = Foo{A,B}
Foo{Int,Float64}() isa Bar{Float64}

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 8, 2020, 12:02pm UTC](https://discourse.julialang.org/t/syntactic-sugar-for-dispatch-on-a-single-type-parameter/42719/3 "2020-07-08T12:02:44Z")

</div>

> [@michakraus](#):
>
> `test(::mystruct{<:Any,<:Any,<:Any}) = false`

This one has a shorter spelling:

```julia-auto
test(::MyStruct) = false

```

No need to write the parameters.

Also, remember that type names should be CapitalCase.

---

<div class="post-metadata">

**Author:** ![michakraus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michakraus/32/222105_2.png) [@michakraus](https://discourse.julialang.org/u/michakraus)\
**Post date:** [July 8, 2020, 6:53pm UTC](https://discourse.julialang.org/t/syntactic-sugar-for-dispatch-on-a-single-type-parameter/42719/4 "2020-07-08T18:53:26Z")

</div>

Neat idea, thanks.

---

<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:** [July 8, 2020, 8:09pm UTC](https://discourse.julialang.org/t/syntactic-sugar-for-dispatch-on-a-single-type-parameter/42719/5 "2020-07-08T20:09:03Z")

</div>

If you always dispatch on the same parameter, place it first?

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [July 8, 2020, 8:18pm UTC](https://discourse.julialang.org/t/syntactic-sugar-for-dispatch-on-a-single-type-parameter/42719/6 "2020-07-08T20:18:41Z")

</div>

You can also write a helper function like this:

```julia
julia> struct mystruct{T1,T2,T3}
       end

julia> mystruct_type(;A=Any,B=Any,C=Any) = mystruct{<:A,<:B,<:C}
mystruct_type (generic function with 1 method)

julia> mystruct_type(B=Nothing)
mystruct{var"#s6",var"#s5",var"#s4"} where var"#s4" where var"#s5"<:Nothing where var"#s6"

julia> f(::mystruct_type(B=Nothing)) = "NoB"
f (generic function with 1 method)

julia> f(::mystruct_type(A=Nothing)) = "NoA"
f (generic function with 2 methods)

julia> f(mystruct{Nothing, Int, Int}())
"NoA"

julia> f(mystruct{Int, Nothing, Int}())
"NoB"

```

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [July 9, 2020, 8:28am UTC](https://discourse.julialang.org/t/syntactic-sugar-for-dispatch-on-a-single-type-parameter/42719/7 "2020-07-09T08:28:09Z")

</div>

> [@michakraus](#):
>
> ```julia-auto
> test(::mystruct{,,<:Nothing}) = true
> 
> ```

You could image writing a macro to allow that, although that exact thing is a syntax error so it won’t work. In a similar spirit, at one point I did write a macro which expands `∷` to `<:Any`, which is at least its fewer characters, e.g.

```julia-auto
@∷ test(::mystruct{∷,∷,<:Nothing}) = true

```

Plus I kind of liked the symmetry with array slicing, like `mystruct{∷,∷,Int}` and `myarray[:,:,1]` which have similar abstract meanings. You could try it, although tbh, I ultimately I hated the look of having to call the macro and didn’t like the extra obfuscation, so I stopped doing it.

---

<div class="post-metadata">

**Author:** ![michakraus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michakraus/32/222105_2.png) [@michakraus](https://discourse.julialang.org/u/michakraus)\
**Post date:** [July 10, 2020, 8:35am UTC](https://discourse.julialang.org/t/syntactic-sugar-for-dispatch-on-a-single-type-parameter/42719/8 "2020-07-10T08:35:41Z")

</div>

> [@baggepinnen](#):
>
> If you always dispatch on the same parameter, place it first?

That’s unfortunately not the case. Otherwise, yes, that would be the obvious thing to do.

---

<div class="post-metadata">

**Author:** ![michakraus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michakraus/32/222105_2.png) [@michakraus](https://discourse.julialang.org/u/michakraus)\
**Post date:** [July 10, 2020, 8:37am UTC](https://discourse.julialang.org/t/syntactic-sugar-for-dispatch-on-a-single-type-parameter/42719/9 "2020-07-10T08:37:29Z")

</div>

> [@jw3126](#):
>
> You can also write a helper function like this:
> 
> ```julia
> [...]
> 
> ```

Thanks a lot. That’s a pretty neat suggestion as well!

> [@marius311](#):
>
> Plus I kind of liked the symmetry with array slicing, like `mystruct{∷,∷,Int}` and `myarray[:,:,1]` which have similar abstract meanings. You could try it, although tbh, I ultimately I hated the look of having to call the macro and didn’t like the extra obfuscation, so I stopped doing it.

I totally agree. This looks pretty cute, but it adds a level of obfuscation, which is probably better avoided. Thanks nonetheless for the suggestion!
