IDL has several places where callback functions are used to provide a mechanism for IDL library routines to call user-defined functions. For example, in curve fitting routines such as
CURVEFIT the callback function allows the user to define custom functions that are evaluated by the fitting routine.
For many callbacks, this interface is perfectly adequate. But for some cases, the callback routine requires some data that is not passed to it in the interface defined by the calling routine. In this case, it is typical for the callback routine to use a technique like common blocks to access the data. But even Fortran recognizes the problems with common blocks; surely there must be a better way to do this in 2015.
IDL 8.5 provides just such a mechanism – function pointers. Objects which inherit from
IDL_Object and define an
_overloadFunction method can be called like a function. This would allow passing a function pointer with the appropriate user-define data as a callback function. A reference to object could be kept and the data could be modified as needed.
This would provide a much nicer interface in a wide variety of applications which use a callback routine: numeric routines such as for curve fitting; several areas in widget programming such as
XMANAGER itself, creating a compound widget, and
WIDGET_TREE drag and drop; and some networking classes such as
The change should not be difficult, just a check for a string versus and object passed as the callback. Something like:
function mg_call_callback, callback, arg1, arg2, _extra=e compile_opt stricter if (size(callback, /type) eq 11) then begin return, callback(arg1, arg2, _extra=e) endif else begin return, call_function(callback, arg1, arg2, _extra=e) endelse end
Of course, this is complicated by the fact that the above routine assumes that there are two positional parameters (though it can handle any keywords), but I will save that for another wish.