# Multidispatch based on value of positional argument

**URL:** https://discourse.julialang.org/t/multidispatch-based-on-value-of-positional-argument/131584
**Category:** New to Julia
**Tags:** dispatch, val
**Created:** [August 13, 2025, 3:16pm UTC](https://discourse.julialang.org/t/multidispatch-based-on-value-of-positional-argument/131584 "2025-08-13T15:16:59Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Snowy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/snowy/32/36765_2.png) [@Snowy](https://discourse.julialang.org/u/Snowy)
#### Post date: [August 13, 2025, 3:16pm UTC](https://discourse.julialang.org/t/multidispatch-based-on-value-of-positional-argument/131584/1 "2025-08-13T15:16:59Z")

</div>

Hi,  
Just to make sure I understand multidispatch correctly here: There is no way to multidispatch based on a value in a positional argument, correct?

For example, something like:

```julia-auto
function multidispatch(x,y,string_argument)
    string_argument = "my_string_1"
    println(x+y)
end

function multidispatch(x,y,string_argument)
    string_argument= "my_string_2"
    println(x*y)
end

multidispatch(5,7,"my_string_1")
12

multidispatch(5,7,"my_string_2")
35

```

Is not possible.

The only thing I could do is have a default without a 3rd argument, and a same function with a 3rd argument, i.e.:

```julia-auto
function multidispatch(x,y)
    println(x+y)
end

function multidispatch(x,y,string_argument)
    string_argument
    println(x*y)
end

multidispatch(7,5)
12

multidispatch(7,5,"hello")
35

```

But this only allows for 2 dispatches, and isn’t really what I’m after.

Thanks for your help

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [August 13, 2025, 3:34pm UTC](https://discourse.julialang.org/t/multidispatch-based-on-value-of-positional-argument/131584/2 "2025-08-13T15:34:35Z")

</div>

Well.. It sort of is, for some types. You can use [`Val`](https://docs.julialang.org/en/v1/base/base/#Base.Val) to lift a runtime value that’s `isbits` or of type `Symbol` to the type domain and dispatch on that in the signature. Things like your `String` won’t work though.

Keep in mind though that this very likely will dispatch dynamically, if the value you’re dispatching on is not known statically.

---

<div class="post-metadata">

### Author: ![Snowy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/snowy/32/36765_2.png) [@Snowy](https://discourse.julialang.org/u/Snowy)
#### Post date: [August 13, 2025, 4:08pm UTC](https://discourse.julialang.org/t/multidispatch-based-on-value-of-positional-argument/131584/3 "2025-08-13T16:08:09Z")

</div>

Thanks!  
With Val, created this example that works correctly:

```julia-auto
function md2(x,y,new_string::Val{Symbol("prod1")})
    new_string
    println(x+y)
end

function md2(x,y,new_string::Val{Symbol("prod2")})
    new_string
    println(x/y)
end

md2(8,4,Val(:prod1))
12
md2(8,4,Val(:prod2))
2.0

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 13, 2025, 4:11pm UTC](https://discourse.julialang.org/t/multidispatch-based-on-value-of-positional-argument/131584/4 "2025-08-13T16:11:52Z")

</div>

Another common pattern is to use custom types just for dispatch:

```julia-auto
julia> struct Prod1 end

julia> struct Prod2 end

julia> md2(x,y,p::Prod1) = x + y
md2 (generic function with 1 method)

julia> md2(x,y,p::Prod2) = x * y
md2 (generic function with 2 methods)

julia> md2(8,4,Prod1())
12

julia> md2(8,4,Prod2())
32

```

---

<div class="post-metadata">

### Author: ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)
#### Post date: [August 13, 2025, 4:35pm UTC](https://discourse.julialang.org/t/multidispatch-based-on-value-of-positional-argument/131584/5 "2025-08-13T16:35:55Z")

</div>

And you could even make these into callable structs to cut down notation a bit (if that is appropriate in the OP’s situation, of course)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 13, 2025, 10:45pm UTC](https://discourse.julialang.org/t/multidispatch-based-on-value-of-positional-argument/131584/6 "2025-08-13T22:45:55Z")

</div>

5 posts were split to a new topic: [How to divide a project into files and submodules?](https://discourse.julialang.org/t/how-to-divide-a-project-into-files-and-submodules/131593)
