Chapter 7. Using implicits and types together
In this chapter
- Introduction to implicit type bounds
- Type classes and their applications
- Type level programming and compile time execution
The type system and the implicit resolution mechanism provide the tools required to write expressive, type-safe software. Implicits can encode types into runtime objects and can enable the creation of type classes that abstract behavior from classes. Implicits can be used to directly encode type constraints and to construct types recursively. Combined with some type constructors and type bounds, implicits and the type system can help you encode complex problems directly into the type system. Most importantly, you can use implicits to preserve type information and delegate behavior to type-specific implementations while preserving an abstract interface. The ultimate goal is the ability to write classes and methods that can be reused anytime they’re needed.
To start, let’s look at some type bounds that we didn’t cover in chapter 6.
Scala supports two types of type constraint operators that aren’t type constraints but are implicit lookups—context bounds and view bounds. These operators allow us to define an implicit parameter list as type constraints on generic types. This syntax can reduce typing in situations where implicit definitions must be available for lookup but don’t need to be directly accessed.