
6 May
2010
6 May
'10
10:34 a.m.
On May 6, 2:13 am, Alexander Solla
Yes, but you can do this without the type class magic, and have the same degree of safety.
data Shape = Square Int | Rectangle Int Int | Circle Float Float | etc... data OpaqueShape = HideShape Shape area :: OpaqueShape -> Float area = ...
These constructs are structurally equivalent. The extra layer of abstraction doesn't really add anything in either case. HideShape (as a function) is an isomorphism onto Shapes in both cases.
You're example isn't as easy to extend. If we want to add shapes we have to edit the original code. We cannot add other shapes at different places in the code. I prefer the type class magic. It is more general and generic.