Type classes

The previous example demonstrated that we need three parts for context bounds to work:

  1. A parameterized type, T, which is defined as an implicit parameter of the function we're going to call
  2. One or more operations (methods) which are defined on T, and which will be available after the conversion
  3. Implicit instances that implement T

In the case of the type referred to of the method definition being an abstract one and the mentioned method being implemented in different ways in instances, we're talking about ad-hoc polymorphism (as opposed to parametric polymorphism for functions, and subtype polymorphism for subclasses). Here, we will explore how this concept is implemented with type classes, how the compiler finds suitable instances if needed, and how variance applies in ad hoc polymorphic cases.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.212.212