Operator implementation
Posted: March 29th, 2011 | Author: Mars | Filed under: Design | Comments OffThe infix operator implementations have always been something of a temporary hack. There was a built in list of arithmetic operations and comparisons for a while, then they became a series of intrinsic functions in the standard library. Over the weekend I reimplemented them in a more generic style. The infix operators are now nothing more than syntactic sugar: each one is associated with a well-known method name, and an object which wants to implement that operator simply provides a method with the appropriate name. This is the same basic idea as the compare_to method I described a couple weeks ago.
The implementations have not changed: the object member invocation syntax is a lookup operation anyway, so I’ve simply made the process of identifying the correct add, subtract, multiply, or divide function explicit instead of hardcoding the linkage into the compiler.
The operator-to-method correspondences are:
+ => add
- => subtract
* => multiply
รท => divide
mod => modulus
** => exponentiate
<< => shift_left
>> => shift_right