Operator overloading

For me it is important to write short, easy to read and easy to understand code. When you write a piece of code and see it after a couple of months or even years the meaning should be clear in an instant. One way to achieve brevity in programming is the use of operator overloading. With this it is possible to change the meaning of an operator.

In this example I’ll take two interpreted languages I use (Python and PHP) and show how those languages differ in the support for operator overloading and why Python has a more elegant solution to this problem.

When you have two “List” objects in PHP it would be nice to just add thoses lists to get a third list that contains the contents of both $l1 and $l2.

class Largelist {
}

$l1 = new Largelist();
$l2 = new Largelist();

$l3 = $l1 + $l2;

But unfortunatly this example below will not work in PHP because the ‘+’ operator is only defined for adding numbers (int and float). Adding two objects will cause a notice “Notice: Object of class Largelist could not be converted to int”. PHP tries to cast everything it finds as operands of the ‘+’ operator to a number.

The only solution in PHP is to add a method that implements the addition of two objects and manually call that method.

class Largelist {
   public function add(other) {
   }
}

$l1 = new Largelist();
$l2 = new Largelist();

$l3 = $l1->add($l2);

But this is a lot longer and less elegant to use and understand. For the ‘+’ operator we seem to feel that it should be associative. This means that a + b = b + a, this goes for numbers so it should also go for objects. Even matrix addition is associative. When using the explicit method add this is less clear. Does $l1->add($l2) == $l2->add($l1)?

In Python this works different because the Python compiler will search for the method __add__ in the class when the ‘+’ operator is used. The class can now implement it’s own method for adding two objects.

class Largelist():
    def __add_