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

By: on September 11, 2007

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.



  1. mikeb says:

    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 :

    “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. matthew says:

    We need a eugenics program for software developers…

  3. Luke says:

    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);

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>