# Best practices for type dispatch?

**URL:** <https://discourse.julialang.org/t/best-practices-for-type-dispatch/3174>\
**Category:** General Usage\
**Created:** [April 11, 2017, 5:12pm UTC](https://discourse.julialang.org/t/best-practices-for-type-dispatch/3174 "2017-04-11T17:12:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [April 11, 2017, 5:12pm UTC](https://discourse.julialang.org/t/best-practices-for-type-dispatch/3174/1 "2017-04-11T17:12:29Z")

</div>

Say I want to create a function that has two methods, each dispatching on a specific (sub)type:

```julia
abstract type AbstractFooBar end

struct Foo <: AbstractFooBar end
struct Bar <: AbstractFooBar end

```

There are (at least) two ways to do this:

```julia
f(::Type{Foo}) = 1
f(::Type{Bar}) = 2

f(Foo) == 1
f(Bar) == 2

```

and

```julia
f(::Foo) = 1
f(::Bar) = 2

f(Foo()) == 1
f(Bar()) == 2

```

Which is most Julia-like?

Also, are there other options available to me? (Don’t assume that there are only two types; in my real-world case, I will have upwards of 20.)

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 11, 2017, 6:10pm UTC](https://discourse.julialang.org/t/best-practices-for-type-dispatch/3174/2 "2017-04-11T18:10:12Z")

</div>

Duplicate. Here’s my answer.

> [@Singleton types vs. instances as type parameters](https://discourse.julialang.org/t/singleton-types-vs-instances-as-type-parameters/2802/2):
>
> Oh yes, this question slight_smile. Fond memories. In JuliaDiffEq, we had a similar problem: what should solve(prob,alg) dispatch on, the type of alg or its instance? We had a month where we: Went with the type Went to the instance Went back to the type Finally at the instance. It has stayed with the instance ever sense. Example: solve(prob,RK4()) The reason is because this is much more flexible. It took me awhile to get here though! Arguments are given here: [https://github.com/Julia…](https://github.com/JuliaDiffEq/Sundials.jl/issues/97)

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [April 11, 2017, 6:24pm UTC](https://discourse.julialang.org/t/best-practices-for-type-dispatch/3174/3 "2017-04-11T18:24:34Z")

</div>

Gah. Sorry about that, @ChrisRackauckas - I didn’t think to search on singleton type.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 11, 2017, 6:28pm UTC](https://discourse.julialang.org/t/best-practices-for-type-dispatch/3174/4 "2017-04-11T18:28:25Z")

</div>

All is well.
