Comparison operator overloading

Posted: March 14th, 2011 | Author: Mars | Filed under: Design, Progress | 1 Comment »

I’ve just redesigned Radian’s suite of comparison operators so they can be implemented by any object. A “comparable” object is one which implements a compare_to method; this method must express the relative ordering of the self object and the method’s parameter. This is essentially the same as the Operator_Compare scheme I developed for REALbasic. I’ve added compare_to methods to the number and symbol atoms, and will do the same for the string atom too.

This came up because the map container needs to sort its keys in some specific order. Obviously the map can’t know about all possible key datatypes ahead of time, so the language needs a standard interface for comparability, and with that in place there’s no reason to implement the comparison operators any other way.


One Comment on “Comparison operator overloading”

  1. 1 Radian Programming Language » Blog Archive » Operator implementation said at 20:35 on March 29th, 2011:

    [...] The 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. [...]