Closures, Visitors, and Hedgehogs

It has been said that there are foxes and there are hedgehogs and "the fox knows many tricks, but the hedgehog knows one great trick." The one great trick of computer science is adding a level of indirection in the right place. There is an interesting similarity between the level of indirection added by the visitor pattern and by closures.

The visitor pattern when used on a type hierarchy allows arbitrary operations to be added independently by third parties after the hierarchy is made, at the upfront cost of an accept method and the visitor interface. Closures when supported in a programming language allow control APIs to be added independently of evolving the language, at the upfront cost of supporting closures in the language. A control API would be something similar to the enhanced for loop in JDK 5 operating on Collections implemented as a library method in java.util rather than a language change.

With both visitors and closures, the construct may be unfamiliar when first seen, but the utility of these patterns make them well worth learning. The extensibility afforded by visitors and closures creates much more scalable environments by allowing functionality to be added without the need for coordination from a single authority, either the type hierarchy or language maintainer.

Comments:

Agreed. It should be noted that these are apples and oranges, as closures are not a design pattern, but a coding technique. The analogue is native multiple dynamic dispatch, which the visitor pattern is a poor man's emulation of.

Posted by Ricky Clarkson on May 14, 2007 at 01:54 AM PDT #

The visitor pattern is a coding pattern most beneficial for single dispatch languages, a point also acknowledged in the GOF book. Closures are a programming language design pattern since they allow abstraction over a different dimension than, say, generics. What capabilities a language natively supports clearly influences what coding patterns work best for that language.

Posted by Joe Darcy on May 14, 2007 at 02:17 AM PDT #

Joe, about the visitor pattern, i secretly hope that invokedynamic will allow write a simpler pattern without all the visitor's limits. Rémi

Posted by Rémi Forax on May 14, 2007 at 04:38 AM PDT #

Rémi, your hope isn't a secret anymore :-) Are the limitations you're referring to the expression problem? The invokedynamic bytecode wouldn't necessarily be generated from any Java source and we'd probably still want to be able to express Java visitors in Java source. The JSR 269 visitors are designed to cope with the specialized instance of the expression problem faced by that API, which should be the subject of one of my future blog posts.

Posted by Joe Darcy on May 14, 2007 at 10:27 AM PDT #

I was not aware that philip wadler wrote something on these problems. I created a small package that use byte-code generation to solve these problems.
About invokedynamic, i hope to generate a bytecode that use it in a similar way java.lang.Proxy does, so it can be accessed from Java but in a safe way.
About JSR269 vistors, i have done something very similar in Tatoo, a parser generator and i wonder why JSR269 visitor doesn't use a type variable for exception, is there a reason ?

Rémi

Posted by Rémi Forax on May 16, 2007 at 10:45 PM PDT #

Rémi,

Wadler coined the term "expression problem" for this issue; he was proud of the dual meaning :-) For JSR 269, we looked an other approaches to addressing the expression problem with generics, but found they added too much complexity without helping the code migration issues.

We did consider how many type parameters the JSR 269 visitors should have. We felt one parameter for a return type was a substantial improvement over no type parameters, a second type parameter for the argument was a much smaller improvement, and a third parameter for exceptions added too much awkwardness for much too small an improvement in expressiveness for an uncommonly needed feature. The GJ paper also recommends two type parameters for visitors while noting a third for exceptions is possible.

Posted by Joe Darcy on May 17, 2007 at 04:32 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

darcy

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
News

No bookmarks in folder

Blogroll