Wed, 28 Apr 2010 13:22:53 -0400, bearophile wrote: > BCS: > >>Could you elaborate on what exactly the expression problem is?< > > I am far from being an expert on such matters, I will do what I can to > answer. It's not an esoteric problem, if you program with an OO language > you have probably faced it sometime.
You failed to mention that the OOP solution is in many cases overly complex. When you don't need to subclass the car elements, a functional pattern matching happens to win - at least if the declarativeness of code matters. The implementation can also use jump tables so it becomes faster than double dispatch. Luckily D is never going to get algebraic data types so you can all stop reading this post right here. data CarElement = Wheel Name | Engine | Body | Car [CarElement] defaultCar = Car [ Wheel "front left" , Wheel "front right" , Wheel "back left" , Wheel "back right" , Body , Engine ] makeVisitor f element = map f (case element of Car elements = element : elements _ = [element] ) visitor element = case element of Wheel name = "Visiting " +++ name +++ " wheel" Engine = "Visiting engine" Body = "Visiting body" Car _ = "Visiting car" doVisitor element = case element of Wheel name = "Kicking my " +++ name +++ " wheel" Engine = "Starting my engine" Body = "Moving my body" Car _ = "Starting my car" -- an example of usage: -- makeVisitor visitor default -- makeVisitor doVisitor default