Re:Rust Traits Deep Dive, Part 2
by kjeetgill on Wednesday June 20, 2018 @06:46AM
(#17354093)
Java can get into some weird cases with generics, super classes, and overriding. Joshua Bloc has some interesting ones in the fantastic (and horrific!) book, "Java Puzzlers: Traps, Pitfalls, and Corner Cases".
For what it's worth, I think python's case is fairly simple and easy to remember.
Python objects can define two different methods for operator * : __mul__(self, b) and __rmul__(self, b). When you see A * B, try A.__mul__(B) if A has a __mul__. If the call throws NotImplemented (usually due to B's type) of if A did not hava a __mul__ call B.__rmul__().
I think that's actually much simpler that relying on types for dispatch since operators tend to be more "flexible" in their operand types than normal functions.
For what it's worth, I think python's case is fairly simple and easy to remember.
Python objects can define two different methods for operator * : __mul__(self, b) and __rmul__(self, b). When you see A * B, try A.__mul__(B) if A has a __mul__. If the call throws NotImplemented (usually due to B's type) of if A did not hava a __mul__ call B.__rmul__().
I think that's actually much simpler that relying on types for dispatch since operators tend to be more "flexible" in their operand types than normal functions.