-
Notifications
You must be signed in to change notification settings - Fork 3
Description
Question regarding 15.10 (Type Classes)
Disclaimer: I didn't fully read the book in order yet, so excuse me in case that my remarks and questions are just an outcome of that.
After reading section 15.10 on Type Classes, I have some trouble understanding the remarks on page 249 and 250 about the zero and fromInt, in the context of the comparison between subtype polymorphism and type classes.
Specifically, this sentence is causing me some trouble:
...you could not rely on subtype polymorphism to implement the
averagefunction because there would be no way to obtain a valuezeroand a functionfromIntfor a given subtypeA.
Question: What does "obtain a value" mean?
As I understand it, if the hypothetical Number supertype had the abstract method fromInt, and zero implemented as fromInt(0) (which is what Numeric does), then I don't see any problem with using subtyping: the concrete subtype like Double or BigDecimal would just implement the fromInt method, to provide the missing functionality. This is the typical Template Method Pattern (which may often be misused in practice).
My point/question is therefore: Am I missing the point? Is this really something one can't do with subtyping, or is it just a different way to structure the code, depending on whether one favors OOP (subtype polymorphism) or FP (ad hoc polymorphism)?
That said, I'm really enjoying the book so far :)