Type class design pattern alternatives

There are, of course, alternatives to the type class design pattern. We can use the adapter design pattern. It will, however, make it much harder to read our code because things will be wrapped all the time and they will be much more verbose. The type class design pattern takes advantage of the nice features of the Scala type system.

Looking at our preceding code, we can also see that there is a fair bit of boilerplate code. This can become problematic in bigger projects, or when we try to define more complex type classes. A library that was written specifically to deal with these issues can be found at https://github.com/mpilquist/simulacrum/.

Get Scala Design Patterns - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.