# Request: someone to complete the Julia example in the Wikipedia page on multiple dispatch

**URL:** <https://discourse.julialang.org/t/request-someone-to-complete-the-julia-example-in-the-wikipedia-page-on-multiple-dispatch/67597>\
**Category:** General Usage\
**Created:** [September 2, 2021, 8:04pm UTC](https://discourse.julialang.org/t/request-someone-to-complete-the-julia-example-in-the-wikipedia-page-on-multiple-dispatch/67597 "2021-09-02T20:04:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [September 2, 2021, 8:04pm UTC](https://discourse.julialang.org/t/request-someone-to-complete-the-julia-example-in-the-wikipedia-page-on-multiple-dispatch/67597/1 "2021-09-02T20:04:30Z")

</div>

The [Wikipedia page on multiple dispatch](https://en.m.wikipedia.org/wiki/Multiple_dispatch) has a complete exsmple in C#, but then in Julia the same exanple is given only as a sketch… could someone that inferstand C# complete the Julia example ? it would then be nice to compare the two implementations…

---

<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:** [September 2, 2021, 8:07pm UTC](https://discourse.julialang.org/t/request-someone-to-complete-the-julia-example-in-the-wikipedia-page-on-multiple-dispatch/67597/2 "2021-09-02T20:07:12Z")

</div>

What are you missing from the example? `return "a/s"` instead of the comment?

It really is that short.

---

<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:** [September 2, 2021, 8:10pm UTC](https://discourse.julialang.org/t/request-someone-to-complete-the-julia-example-in-the-wikipedia-page-on-multiple-dispatch/67597/3 "2021-09-02T20:10:59Z")

</div>

Since it’s not that long:

```julia
abstract type SpaceObject end
struct Asteroid <: SpaceObject
    size::Int
end
struct Spaceship <: SpaceObject
    size::Int
end

collide_with(x::Asteroid, y::Asteroid) = "a/a"
collide_with(x::Asteroid, y::Spaceship) = "a/s"
collide_with(x::Spaceship, y::Asteroid) = "s/a"
collide_with(x::Spaceship, y::Spaceship) = "s/s"
collide(x::SpaceObject, y::SpaceObject) = x.size > 100 && y.size > 100 ? "Big Boom!" : collide_with(x,y)

```

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [September 2, 2021, 8:11pm UTC](https://discourse.julialang.org/t/request-someone-to-complete-the-julia-example-in-the-wikipedia-page-on-multiple-dispatch/67597/4 "2021-09-02T20:11:30Z")

</div>

It was more than the whole program, but I think I now got the logic of the C# implementation…

---

<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:** [September 2, 2021, 8:14pm UTC](https://discourse.julialang.org/t/request-someone-to-complete-the-julia-example-in-the-wikipedia-page-on-multiple-dispatch/67597/5 "2021-09-02T20:14:17Z")

</div>

The problem with these sort of short examples is that they’re only using regular method overloading since we’re in a single namespace. Multiple dispatch really only begins to come in handy when you can define a new method with new types for a function of a different namespace, that didn’t originally knew about these types.

---

<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:** [September 2, 2021, 8:17pm UTC](https://discourse.julialang.org/t/request-someone-to-complete-the-julia-example-in-the-wikipedia-page-on-multiple-dispatch/67597/6 "2021-09-02T20:17:21Z")

</div>

E.g. the examples from this juliacon talk are much better in my opinion - they show how bringing together multiple different namespaces (that don’t inherently know about each other) can work because of multiple dispatch:

[![](https://global.discourse-cdn.com/julialang/original/3X/c/5/c5a2f02c81c376da005f8610ce9cdd4ad47cfdf4.jpeg "JuliaCon 2019 | The Unreasonable Effectiveness of Multiple Dispatch | Stefan Karpinski") ](https://www.youtube.com/watch?v=kc9HwsxE1OY)

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [September 2, 2021, 8:18pm UTC](https://discourse.julialang.org/t/request-someone-to-complete-the-julia-example-in-the-wikipedia-page-on-multiple-dispatch/67597/7 "2021-09-02T20:18:41Z")

</div>

Would it be correct to say that the advantage derives from the fact that high level functions can delegate specialisation to lower level ones? So if the lower-level methods are implemented for the new type, the high level methods would be also fine with it… ?

---

<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:** [September 2, 2021, 8:26pm UTC](https://discourse.julialang.org/t/request-someone-to-complete-the-julia-example-in-the-wikipedia-page-on-multiple-dispatch/67597/8 "2021-09-02T20:26:25Z")

</div>

No, that’s just a sideeffect due to how the C# example has to be structured because of the way everything has to be inside of a class in C#.

E.g. I could extend the `collide` function like so:

```julia
collide(a::Asteroid, b::SpaceObject) = a.size >= 1_000_000_000 ? "Extinction event, have a nice day!" : collide_with(a, b)

```

and the other implementation on two `::SpaceObject`s wouldn’t care:

```julia
julia> collide(Asteroid(1), Spaceship(1))            
"a/s"                                                
                                                     
julia> collide(Asteroid(1_000_000_000), Spaceship(1))
"Extinction event, have a nice day!"                 
                                                     
julia> collide(Spaceship(1_000_000_000), Asteroid(1))
"s/a"                                                

```

the first two calls call the new method since the first argument is an `Asteroid`. Before defining that new specialized method `collide(::Asteroid, ::SpaceObject)`, we would have gotten `"a/s"` for either call because only one of the two space objects has a size greater than `100`.

–

That said, since every function in julia has multiple dispatch, this works on all levels - high level API as well as low level workhorse functions.
