Switch statements versus polymorphism
c2.com
c2.com
There's some very interesting research on resolving this issue: http://www.cs.ucla.edu/~todd/research/icfp02.html
"Clojure supports sophisticated runtime polymorphism through a multimethod system that supports dispatching on types, values, attributes and metadata of, and relationships between, one or more arguments."
It exchanges one form of duplication for another, not eliminates it.
The real "problem" is that textual code is generally one-dimensional in nature, but we are interweaving two dimensions of option combos. It's allegedly a limit inherent to textual code, regardless of paradigm used to write the code.
I'm currently thinking of language with "code" laying in database of sort, and text is just a view of real data. Of course, there would be some text serialization, but it wouldn't be the "true way" for programming it...
Consider the following (scheme) definition for the ``eval'' function. (This is a conditional and not a case statement, but it's still type-based dispatch, so I think it qualifies)
(define (eval exp env)
(cond ((self-evaluating? exp) exp)
((variable? exp) (lookup-variable exp env))
((assignment? exp) (eval-assignment exp env))
((definition? exp) (eval-definition exp env))
... ; you get the picture
))
The input to this is some scheme expression. If it's a number or string (for example), that's the result (it's self evaluating). On the other hand, if it starts with the symbol ``define'', it's a definition and we define the variable in the environment.Scheme doesn't support polymorphism, but as far as I can tell, even if it did this couldn't be changed to be polymorphic without changing what the function actually does. At the end of the day, if the input to your function is diverse enough, you're probably going to have to perform some sort of dispatch based on the type of that input.
On the other hand, I have a fairly small amount of experience with OO code, so I could be totally off base here... (please let me know if that's the case).