Implicit Type Conversions

The compile-time type checking is quite rigorous in Scala. If you don’t pass the type expected by a function or invoke an arbitrary method on an instance, you’ll get a stern error at compile time. While this is good most of the time, occasionally you’d want to pass an instance of one type where another type is expected—mainly where such use will make code intuitive, more expressive, and easy to write. Likewise, you may want to invoke your own domain-specific convenient methods on third-party classes. This can give an illusion of greater power to open and add methods to existing third-party classes. All this is possible in Scala with just a few tricks by using type conversions.

There are two different ways to implement ...

Get Pragmatic Scala 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.