# How to dispatch according to the number I put in the type parameter?

**URL:** <https://discourse.julialang.org/t/how-to-dispatch-according-to-the-number-i-put-in-the-type-parameter/27071>\
**Category:** General Usage\
**Tags:** question, parametric-types\
**Created:** [August 1, 2019, 9:14am UTC](https://discourse.julialang.org/t/how-to-dispatch-according-to-the-number-i-put-in-the-type-parameter/27071 "2019-08-01T09:14:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [August 1, 2019, 9:14am UTC](https://discourse.julialang.org/t/how-to-dispatch-according-to-the-number-i-put-in-the-type-parameter/27071/1 "2019-08-01T09:14:29Z")

</div>

```julia
struct MM{N}
end

f(x::MM{N}) where N>5 = println("GREATER THAN 5")
f(x::MM{N}) where N<3 = println("LESS THAN 3")

f(MM{2}())

```

I want to dispatch according to the number I put in the type parameter. Is there a way to do this?

Thanks

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [August 1, 2019, 9:17am UTC](https://discourse.julialang.org/t/how-to-dispatch-according-to-the-number-i-put-in-the-type-parameter/27071/2 "2019-08-01T09:17:50Z")

</div>

```julia
f(::Val{3}) = println("is 3")
f(::Val{5}) = println("is 5")
g(x::Int) = f(Val(x))

```

and then:

```julia
julia> g(3)
is 3

julia> g(5)
is 5

```

But please read this first:  
[https://docs.julialang.org/en/v1/manual/performance-tips/#Types-with-values-as-parameters-1](https://docs.julialang.org/en/v1/manual/performance-tips/#Types-with-values-as-parameters-1)

---

<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 1, 2019, 9:19am UTC](https://discourse.julialang.org/t/how-to-dispatch-according-to-the-number-i-put-in-the-type-parameter/27071/3 "2019-08-01T09:19:04Z")

</div>

You can use traits (my example is with just one branch):

```julia
struct MM{N} end
_trait(::MM{N}) where {N} = Val{N ≤ 3}()
f(x::MM) = _f(_trait(x))
_f(::Val{true}) = println("≤ 3")
_f(::Val{false}) = println("> 3")

```

(ps: please use more informative topic names)

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [August 1, 2019, 9:19am UTC](https://discourse.julialang.org/t/how-to-dispatch-according-to-the-number-i-put-in-the-type-parameter/27071/4 "2019-08-01T09:19:21Z")

</div>

However this is not something I want, if you look at the where statement, you can see that MM{6},MM{7} will dispatch to the first one. I want to create ranges …

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [August 1, 2019, 9:19am UTC](https://discourse.julialang.org/t/how-to-dispatch-according-to-the-number-i-put-in-the-type-parameter/27071/5 "2019-08-01T09:19:53Z")

</div>

Value based dispatch…  
I like it.  
Thanks

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [August 2, 2019, 1:11pm UTC](https://discourse.julialang.org/t/how-to-dispatch-according-to-the-number-i-put-in-the-type-parameter/27071/6 "2019-08-02T13:11:13Z")

</div>

Note that for simple logic like this, the compiler is smart enough to eliminate unused branches, so you can just write:

```julia
function f(::MM{N}) where N
    if N > 5
        return println("GREATER THAN 5")
    elseif N < 3
        return println("LESS THAN 3")
    end
end

```

and it will be just as efficient as the other proposed solutions. For more complex cases, you have be a bit careful, that you’re not calling functions with side effects in your logic, otherwise the compiler won’t be able to specialize on `N` and there will be some overhead.
