Best way to implement +(x::Float32,y::Float32) in 2 different modules?

I wouldn’t say that, and I don’t think this has anything to do with OO. The issue you are having has to do with your mental model of how to look up the valid methods for a given function name. What I would say is that there is a worldview clash between people coming from C++/Java/Python/C#/etc. and Lisp - where many of the core developers using Julia come from a Lisp mindset.

If you come from an OO (i.e. single-dispatch) world it is immediately obvious that the namespace of the type provides valid methods to operate on that type (hence, you can always keep operator+ in the namespace of the type it applies to in any OO language), and if you come from function overloading (i.e. C++) then there is ADL which means the compiler looks in the namespace of any of the types in the signature of the function.

If you read through the later parts of Function name conflict: ADL / function merging? you will finally see that we figured out why namespaces in Julia are so confusing to people from C++/Java/Python/C# and make complete sense to people who used CLOS. Hopefully there will be a way to reconcile the worlds.

In principle you could put a :+ wherever you want. In practice you are going to have to put it in the Base module. As for other functions, if you want to easily use the same function name on completely different type signatures, then you generally have to find a shared namespace to put them in. Finally, if you want to use any of the function names that are used in Base for your own types, then you probably want to just put those functions in Base to make your life easier. It may be ugly, but it is a reasonable medium-term solution… (and longterm solution if the Lisp-style thinking on function lookup is maintained).

But, in your exact example, I am not sure if a C+±style ADL approach would work either (at least for the operator+ part. The plus part would be in C++ fine since the signatures operate on distinct types.

This has nothing to do with OO, nor does it exclude multiple-dispatch. It is just that people who come from an OO language expect there to be some sort of argument dependent lookup of valid methods by looking at the namespace of the type it is called on (even if they don’t realize that is how single-dispatch languages work).

Keep in mind that in C++, and other languages with function overloading, it is kind of like multiple-dispatch with the type is known at compile-time. Eventually, C++ may also even have something that does that dynamically (http://www.stroustrup.com/multimethods.pdf), just like in julia. The name lookup rules are orthogonal to all of this (even if the implementation can get tricky in a more dynamic language).

2 Likes