Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I wrote two articles which may clarify this better. One is about creating custom temperature units in Julia and Python. It shows how multiple dispatch gives an advantage over single dispatch: https://medium.com/@Jernfrost/defining-custom-units-in-julia...

This story is more like a general intro to Julia, but it has an example with Knights, Pikeman and Archers fighting each other (sort of rock, paper, scissors game), which also shows the utility of multiple dispatch.

https://levelup.gitconnected.com/knights-pikemen-archers-and...

But to give a quick idea here. Imagine writing functions for intersecting two geometric shapes. The algorithm for intersecting a circle and square is entirely different from intersecting a polygon and a line segment. The specific algorithm needed will depend on BOTH shapes not just one. Python can only dispatch on one of the shapes, not both.

So in Julia I could write a different function implementation like this:

    intersect(c::Circle, r::Rectangle)
    intersect(c1::Circle, c2::Circle)
    intersect(r1::Rectangle, r2::Rectangle)
and so on.


I appreciate your first article demonstrating the differences between Python's single dispatch and Julia's multiple dispatch, but your python example suffers from a lack of a child-aware parent class, Temperature, that could resolve the complaints raised. You're building inherently coupled classes in a decoupled way and complaining that they don't mix well.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: