Skip to content

Question regarding 15.10 (Type Classes) #4

Answered by charpov
edufuga asked this question in Q&A
Discussion options

You must be logged in to vote

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…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@edufuga
Comment options

@edufuga
Comment options

@charpov
Comment options

Answer selected by charpov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #3 on September 22, 2023 12:41.