Functions are data (and very cool data, too)
jaspervdj.be
jaspervdj.be
Using functions to represent objects like this is rather silly because they are opaque and only support a single operation.
Here are a number of possibly better ways to do it:
- Emulate objects with functions and messages like with scheme or lisp.
- Use structural typing or polymorphic variants like in OCaml.
- Use records with function fields.
- Use a variant union.
- Extend/build a list monad over your class.
Can you think of other or better ways?Basically, the original post is just another newbie FP realising that - hey - if you're only every going to call one function of a class then the class is completely defined by that function. He presents a poor and incomplete solution.
There are much better blog posts on FP design than this. Please don't upvote this one.
The more normal solution is to make a single data type which is a disjunction of all the shapes which you want to deal with.