September 2018
Intermediate to advanced
398 pages
9h 43m
English
To summarize, if we want to create a type class, we have to perform the following steps:
Create trait, MyTypeClass[A], that accepts a parameterized type A. It represents the type class interface.
Define an apply[A] function in the companion object of MyTypeClass, in order to facilitate the instantiation of type class instances.
Provide implicit instances of trait for all of the desired types (Int, String, Option, and so on).
Define an implicit conversion to an Ops class, so that we can call methods of the type class as if they were declared in the target type (like you saw previously, with 2.combine(3)).
These definitions can be written manually, like in the previous section. Alternatively, you can use simulacrum ...
Read now
Unlock full access