3 ms·
Julia shines with this style, especially since the way modules compose works much better with multiple dispatch that crosses module boundaries. Sometimes a few
by sriku 6y ago
Julia shines with this style, especially since the way modules compose works much better with multiple dispatch that crosses module boundaries. Sometimes a few surprises crop up, but by and large it's such a pleasure to work with.
- childintime 6y agoSeeing is believing: could you translate an example to Julia?
- ddragon 6y agoThat example in Julia would look the exact same since it's a very simple example with just one object/struct and only one version of each function (so it doesn't even need function overloading, let alone multiple dispatch), which is also why it might not be a good example against Python OOP, even though I'm inclined to agree, since it doesn't even need single dispatch (which if needed can be achieved by functools.singledispatch in Python). The difference would start when you get later a different entity like for example an Organization. Now you have to either create a save_items_org (and check which one it is to choose the correct method) or you add the same check to save_items(Union[Client,Organization]...) and all other methods. If you had used the object then it would be client.save_items() or org.save_item(). Using functools.singledispatch or Julia, save_items(org...) and save_items(client...) will work like the object version and dispatch without trouble and keep as concise as the object version. Now, the difference with Julia, is that Python's native dispatch capability ends here. If you now want a method to link Users with Users, Users with Organizations and Organizations with Organizations (sorry for the weird usecase, I don't want to add more types) you can just implement in Julia a link(Users, Organization), link (Organization, Organization), link(User, User) and any combination and whatever combination of User and Organization you receive it will dispatch to the correct version (while in Python you'll be forced to do the runtime check with an if to choose the implementation). For example the product operator (*) in Julia has around 300 implementations for all kinds of combinations of arguments, and the compiler makes it extremely fast by inferring and doing static dispatch.
- sriku 6y ago@ddragon described it rather well. Some significant examples include JuMP (https://jump.dev https://jump.dev) for mathematical optimization and how FluxML (Julia's "Keras"), Zygote (whole program automatic differentiation) and ODE solvers can work together easily to build Neural ODE solvers in relatively few lines of code. It is also why one person can write - https://github.com/yuehhua/GeometricFlux.jl https://github.com/yuehhua/GeometricFlux.jl - and benefit from the CUDA, FluxML, Zygote packages to get high performance.