The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Mark Elston (133) [Avatar] Offline
In section 12.2 you use the 'instance' keyword but don't really explain it. The word implies something (to me) that I don't think is correct so I have trouble understanding how to read the listings that make use of it or what it is actually doing.

When I see 'instance' I think of an instance of a class - an object. Clearly that isn't what is intended here. In listing 12.3 the word is not used to define any particular object. Instead it seems to be defining methods of a class. But that 'class' was defined elsewhere.

So, it seems that 'instance' is used to 'attach' functions to type classes (almost like defining methods in a class). But it doesn't have to do it where the 'class' is defined. This is somewhat confusing as it makes the management of a type class spread out over, potentially, many files.

I would like to see more description of what is really happening with this keyword and how it is supposed to be used.
Will Kurt (21) [Avatar] Offline
Looking over that section again you're absolutely correct that I don't give this keyword enough attention. I'll go back and review that in the future to make it more clear.

In Haskell the 'instance' keyword is used to make a type an "instance" of a type class, which is definitely quite different than the way "instance" is used in OOP. Coming from an OOP background it might be helpful to think of "instance" in Haskell being closer to "implements". To draw parallels with OOP, Haskell type classes are very similar to Interfaces. Making a Haskell type an instance of a type class, is the roughly the same as making a Java Class implement an Interface.

Let me know if that helps clarify things and if it does I'll add a similar note to the lesson.

Thanks for commenting, and apologies to getting back to this (and your other questions) so late, I was traveling last week.
Mark Elston (133) [Avatar] Offline
That does help, thanks. It is where I was heading on my own but it is good to have it confirmed.