Operator overloading is the feature in IDL 8.0 that I have been testing the most; I am already using it in a couple of my projects. The behavior of objects when used in expressions containing nearly any of IDL’s operators can be defined through special methods of the object.

To make an object which recognizes operators, simply inherit from IDL_Object and write a special method for each operator you wish to use. For example, to make your object interact with the + operator, simply write a method with the signature

function myclass::_overloadPlus, left, right

When an object of your class is one of the operands of an expression using +, as in result = left + right, the special method will be called to compute result.

In addition to the standard unary and binary operators like +, le, unary -, etc., objects can also interact with other special routines and language keywords such as HELP, PRINT, SIZE, and FOREACH. Furthermore, the object can define behavior when it is accessed with []. It also automatically accesses properties when . is used (calling getProperty and setProperty as appropriate).

As an example of using operator overloading, I have some classes and routines for accessing HDF5 files (download). To see these classes in use, checkout the main-level program at the end of mg_h5.pro. Run it with:

ID> .run mg_h5

First, I have a convenience function mg_h5 which creates an object representing an HDF 5 file, an MGffHDF5File object:

IDL> h = mg_h5(file_which('hdf5_test.h5'))

The HELP routine will now present more information:

IDL> help, h
H MGFFHDF5F = </Applications/itt/idl/idl80/examples/data/hdf5_test.h5>

To make this work, I had to write the following special method:

function mgffhdf5file::_overloadHelp, varname
  compile_opt strictarr

  type = obj_class(self)
  specs = string(self.filename, format='(%"<%s>")')
  return, string(varname, type, specs, format='(%"%-15s %-9s = %s")')
end

An HDF 5 file is hierarchical, so groups in the file can be accessed using []:

IDL> g1 = h['images']

The method to implement this is a bit more complicated, but the signature is:

function mgffhdf5file::_overloadBracketsRightSide, isRange, $
                                                   ss1, ss2, ss3, ss4, $
                                                   ss5, ss6, ss7, ss8

Here, isRange is an array indicating whether each dimension specified using [] is a range (i.e., something like 0:10) or just a particular value (like 5 or 'images'). Then ss1 to ss8 are either 3 element arrays or scalar values, depending on the corresponding value in isRange.

One trick: it is possible to write classes that provide the ability to use operator overloading when used in IDL 8.0, but provide their normal functionality when used with IDL versions before 8.0. Just write your own IDL_Object class (here’s mine). This will not be found when using IDL 8.0, since its IDL_Object is a core built-in class, but will be found in older versions of IDL so that classes which inherit from it will compile.

I think that, used wisely, operator overloading can provide very usable, intuitive objects. But too much operator overloading can lead to overly complicated code which is difficult to debug.