Tutorial by Examples

An implicit conversion allows the compiler to automatically convert an object of one type to another type. This allows the code to treat an object as an object of another type. case class Foo(i: Int) // without the implicit Foo(40) + 2 // compilation-error (type mismatch) // defines how t...
Implicit parameters can be useful if a parameter of a type should be defined once in the scope and then applied to all functions that use a value of that type. A normal function call looks something like this: // import the duration methods import scala.concurrent.duration._ // a normal method...
Implicit classes make it possible to add new methods to previously defined classes. The String class has no method withoutVowels. This can be added like so: object StringUtil { implicit class StringEnhancer(str: String) { def withoutVowels: String = str.replaceAll("[aeiou]", &quo...
Assuming an implicit parameter list with more than one implicit parameter: case class Example(p1:String, p2:String)(implicit ctx1:SomeCtx1, ctx2:SomeCtx2) Now, assuming that one of the implicit instances is not available (SomeCtx1) while all other implicit instances needed are in-scope, to creat...
To view all the implicits in-scope during a REPL session: scala> :implicits To also include implicit conversions defined in Predef.scala: scala> :implicits -v If one has an expression and wishes to view the effect of all rewrite rules that apply to it (including implicits): scala> ...

Page 1 of 1