Closing over context still not easy in mainstream languages, Film at 11
September 11th, 2007 tonyg
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.
Entry Filed under: Technology, Rant
3 Comments Add your own
1. mikeb | September 11th, 2007 at 1:32 pm
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. matthew | September 11th, 2007 at 2:58 pm
We need a eugenics program for software developers…
3. Luke | September 11th, 2007 at 4:39 pm
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!
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed