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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.