technology from back to front

Closing over context still not easy in mainstream languages, Film at 11

I find it fascinating that after so many decades of support for closures, we’re still stuck in a C-style mentality of passing function-pointers that take an explicit context argument rather than a proper closure object. Witness the design of .NET’s Type.FindInterfaces method:

public virtual Type[] FindInterfaces (TypeFilter filter,
                                      Object filterCriteria);

The TypeFilter argument is a delegate. The Object argument is context that the delegate may require! This is pretty much exactly the old-school C-style way of implementing closures:

/* Yes, pretty crude translation, I know */
TypeArray find_interfaces(int (*type_filter)(Type*, void*),
                          void *argument);

Smalltalk (and Lisps) would do it in the natural way, with a block (a closure):

someType selectInterfaces: [:interface | ... ]

Lisp 1.5, complete with support for lexical closures, appeared in 1959. It’s 2007. That’s forty-eight years.

by
tonyg
on
11/09/07
  1. Yes, it’s comical, but why do you think it is so?

    Take a look at the resistance to introducing things like lambda forms to JavaScript; for example, at http://ejohn.org/blog/javascript-18-progress/ :

    “All new stuffs since JS1.7 make javascript code less and less readable and comprehensible by a “normal” developer …”

    “I think the “new style” of coding is hard to read, understand and debug …”

    .. and that’s /JavaScript/.

  2. We need a eugenics program for software developers…

  3. This method was around during C# 1.0, when there were no closures. An alternative to what you have shown would be to have FindInterfaces take a class instance (common for Java IIRC). If this method were updated for C# 2.0+, it could simply take a Predicate. You should probably aim your criticism at Java. :-p C# 3.0 syntax would look like this:

    someType.FindInterfaces(iface => iface.Name == "IFoolishness");

    Pretty readable if you ask me!

  4. c# 3.0 lambda expression is:

    ob.GetType().FindInterfaces((iface, criteria) => iface.FullName == criteria.ToString(), typeof(IMapas).FullName);

 
 


four × 3 =

2000-14 LShift Ltd, 1st Floor, Hoxton Point, 6 Rufus Street, London, N1 6PE, UK+44 (0)20 7729 7060   Contact us