[Haskell-cafe] Attempt to emulate subclasses in Haskell, am I reinventing the wheel?

Clinton Mead clintonmead at gmail.com
Mon Apr 20 18:01:43 UTC 2015

I've linked to an ugly attempt to emulate subclasses in Haskell, and I'm
wondering if anyone has done what I have perhaps in a cleaner way.

Firstly, the code is here: http://ideone.com/znHfSG

To explain what I've done, I first thought that a "method" basically takes
some "input" (which I've called "i"), an object (which I've called "c" for
class) and returns some output "o" and a potentially modified object "c".
I've captured this behaviour in the badly named class "C".

I've then made a class "User", with methods "getFirstName" and
"putFirstName" and defined them appropriately.

Furthermore, I've then made a data type "Age", and then "ExtendedUser"
which combines "User" with "Age".

At this point, I can still call "getFirstName" and "putFirstName" on
"ExtendedUser", as would be hoped.

I also defined "getAge", which naturally works on ExtendedUser.

Furthermore, I can override "getFirstName" on "ExtendedUser", which I have
done to instead return a capitalised version.

Is what I've done of any practical use? And has someone done it better than
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150421/269e7af5/attachment.html>

More information about the Haskell-Cafe mailing list