-
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 Specifically, this sentence is causing me some trouble:
Question: What does "obtain a value" mean? As I understand it, if the hypothetical 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 :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
My answer to your question: Both! Type classes are an alternative for FP languages that don't support subtype polymorphism, such as Haskell. Many problems can be solved with one or the other. Still, there are things that one does better than the other. For instance, uniform processing of a heterogeneous collection, as in Listing 15.4, is a natural use of subtype polymorphism, but is awkward to write using type classes. On the other hand, the average function from Listing 15.15 cannot be written with subtype polymorphism. The reason is that a type class lets you associate values and functions with the type itself, which has no equivalent with subtype polymorphism. In Scala, The closest I can get would be something like this: interface Number<A> {
A plus(A x);
A divideByInt(int n);
} public static <A extends Number<A>> A average(List<A> numbers) {
if (numbers.isEmpty()) throw new IllegalArgumentException();
var i = numbers.iterator();
A sum = i.next();
while (i.hasNext())
sum = sum.plus(i.next());
return sum.divideByInt(numbers.size());
} This works fine, but cannot handle empty lists. Adding a By the way, while the mechanics were always easy to understand, it took me a while to grasp the benefits and proper use of type classes. It's when I faced the task of writing common tests for two separate (and type-unrelated) implementations of the same functionalities that I finally understood how I could benefit from type classes. They let you bridge unrelated types and bring new types into a pre-existing framework (like type |
Beta Was this translation helpful? Give feedback.
My answer to your question: Both!
Type classes are an alternative for FP languages that don't support subtype polymorphism, such as Haskell. Many problems can be solved with one or the other. Still, there are things that one does better than the other. For instance, uniform processing of a heterogeneous collection, as in Listing 15.4, is a natural use of subtype polymorphism, but is awkward to write using type classes. On the other hand, the average function from Listing 15.15 cannot be written with subtype polymorphism. The reason is that a type class lets you associate values and functions with the type itself, which has no equivalent with subtype polymorphism.
In Scala,
Numeric
is a ty…