# Should I dispatch on singleton types or their instances?

**URL:** <https://discourse.julialang.org/t/should-i-dispatch-on-singleton-types-or-their-instances/28204>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [August 30, 2019, 10:40am UTC](https://discourse.julialang.org/t/should-i-dispatch-on-singleton-types-or-their-instances/28204 "2019-08-30T10:40:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [August 30, 2019, 10:40am UTC](https://discourse.julialang.org/t/should-i-dispatch-on-singleton-types-or-their-instances/28204/1 "2019-08-30T10:40:46Z")

</div>

When I am developing packages, I often come across the following problem.

```julia
struct Step{T} end

func(::Step{1}, x) = do something...
func(::Step{2}, x) = do some different things...

```

I am not sure should I use singleton types `Type{Step{1}}` and `Type{Step{2}}`, instead of singletons `Step{1}` and `Step{2}` to dispatch behaviors?

```julia
func(::Type{Step{1}}, x) = do something...
func(::Type{Step{2}}, x) = do some different things...

```

Julia’s doc says [instances should be used](https://docs.julialang.org/en/v1/manual/types/#%22Value-types%22-1), is it a general rule or just for `Val` type? Because I have seen both of them be used in different packages:

1. Using [instances](https://github.com/JuliaMath/Roots.jl) (like `Bisection()`)

```julia
using Roots

# struct Bisection <: AbstractBisection end # either solvable or A42
f(x) = exp(x) - x^4

# a bisection method has the bracket specified with a tuple or vector
julia> find_zero(f, (8,9), Bisection())
8.613169456441398

```

1. Using [singleton types](https://juliaintervals.github.io/IntervalRootFinding.jl/latest/roots/) (like `Newton`):

```julia
using IntervalRootFinding

roots(log, -2..2, Newton)

```

1. Which one is a better practice?
2. Can those types (`Step`, `Bisection`, `Newton`) be called “traits”?
3. If we use singleton types (`Type{Step}`), should I just let the type `Step` be an abstract type, instead of a concrete type?

---

<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:** [August 30, 2019, 10:55am UTC](https://discourse.julialang.org/t/should-i-dispatch-on-singleton-types-or-their-instances/28204/2 "2019-08-30T10:55:46Z")

</div>

Often the context suggests a preferred method: eg if the type is a container but is meaningful for some method without its contents (eg `convert`), or the contents are not relevant for some kind of information (`Base.IteratorSize`), or no value is available at some point (eg you are just creating an instance).

When in doubt, I would go for instances, not types. YMMV.

I think that these patterns now transcend the original idea of traits, but I guess one could still call them traits.

---

<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:** [August 30, 2019, 11:22am UTC](https://discourse.julialang.org/t/should-i-dispatch-on-singleton-types-or-their-instances/28204/3 "2019-08-30T11:22:21Z")

</div>

> [@Tamas\_Papp](#):
>
> When in doubt, I would go for instances, not types.

I agree, you never know when you want to add options to the type in the form of fields

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [August 30, 2019, 11:30am UTC](https://discourse.julialang.org/t/should-i-dispatch-on-singleton-types-or-their-instances/28204/4 "2019-08-30T11:30:10Z")

</div>

> [@Tamas\_Papp](#):
>
> Often the context suggests a preferred method: eg if the type is a container but is meaningful for some method without its contents (eg `convert` ), or the contents are not relevant for some kind of information ( `Base.IteratorSize` ), or no value is available at some point (eg you are just creating an instance).

Sorry I do not quite get it. You mean at these circumstances we should use types rather than instances?

---

<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:** [August 30, 2019, 11:36am UTC](https://discourse.julialang.org/t/should-i-dispatch-on-singleton-types-or-their-instances/28204/5 "2019-08-30T11:36:17Z")

</div>

Yes, I suggested these as examples where using types makes more sense.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 30, 2019, 12:30pm UTC](https://discourse.julialang.org/t/should-i-dispatch-on-singleton-types-or-their-instances/28204/6 "2019-08-30T12:30:35Z")

</div>

Related: [Singleton types vs. instances as type parameters](https://discourse.julialang.org/t/singleton-types-vs-instances-as-type-parameters/2802)

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [September 1, 2019, 3:15pm UTC](https://discourse.julialang.org/t/should-i-dispatch-on-singleton-types-or-their-instances/28204/7 "2019-09-01T15:15:59Z")

</div>

FWIW the style guide in the manual recommends using instances:  
[https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-confusion-about-whether-something-is-an-instance-or-a-type-1](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-confusion-about-whether-something-is-an-instance-or-a-type-1)
