|
| 1 | +--- |
| 2 | +layout: overview |
| 3 | +title: Implicit Classes |
| 4 | +label-color: success |
| 5 | +label-text: Available |
| 6 | +--- |
| 7 | + |
| 8 | +**Josh Suereth** |
| 9 | + |
| 10 | +## Introduction |
| 11 | + |
| 12 | +Scala 2.10 introduced a new feature called *implicit classes*. An *implicit class* is a class |
| 13 | +marked with the `implicit` keyword. This keyword makes the class' primary constructor available |
| 14 | +for implicit conversions when the class is in scope. |
| 15 | + |
| 16 | +Implicit classes were proposed in [SIP-13](http://docs.scala-lang.org/sips/pending/implicit-classes.html). |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +To create an implicit class, simply place the the `implicit` keyword in front of an appropriate |
| 21 | +class. Here's an example: |
| 22 | + |
| 23 | + object Helpers { |
| 24 | + implicit class IntWithTimes(x: Int) { |
| 25 | + def times[A](f: => A): Unit = { |
| 26 | + def loop(current: Int): Unit = |
| 27 | + if(current > 0) { |
| 28 | + f |
| 29 | + loop(current - 1) |
| 30 | + } |
| 31 | + loop(x) |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | +This example creates the implicit class `IntWithTimes`. This class wraps an `Int` value and provides |
| 37 | +a new method, `times`. To use this class, just import it into scope and call the `times` method. |
| 38 | +Here's an example: |
| 39 | + |
| 40 | + scala> import Helpers._ |
| 41 | + import Helpers._ |
| 42 | + |
| 43 | + scala> 5 times println("HI") |
| 44 | + HI |
| 45 | + HI |
| 46 | + HI |
| 47 | + HI |
| 48 | + HI |
| 49 | + |
| 50 | +For an implicit class to work, its name must be in scope and unambiuous, like any other implicit |
| 51 | +value or conversion. |
| 52 | + |
| 53 | + |
| 54 | +## Restrictions |
| 55 | + |
| 56 | +Implicit classes have the following restrictions: |
| 57 | + |
| 58 | +**1. They must be defined inside of another `trait`/`class`/`object`.** |
| 59 | + |
| 60 | + |
| 61 | + object Helpers { |
| 62 | + implicit class RichInt(x: Int) // OK! |
| 63 | + } |
| 64 | + implicit class RichDouble(x: Double) // BAD! |
| 65 | + |
| 66 | + |
| 67 | +**2. They may only take one non-implicit argument in their constructor.** |
| 68 | + |
| 69 | + |
| 70 | + implicit class RichDate(date: java.util.Date) // OK! |
| 71 | + implicit class Indexer[T](collecton: Seq[T], index: Int) // BAD! |
| 72 | + implicit class Indexer[T](collecton: Seq[T])(implicit index: Index) // OK! |
| 73 | + |
| 74 | + |
| 75 | +While it's possible to create an implicit class with more than one non-implicit argument, such classes |
| 76 | +aren't used during implicit lookup. |
| 77 | + |
| 78 | + |
| 79 | +**3. There may not be any method, member or object in scope with the same name as the implicit class.** |
| 80 | + |
| 81 | +*Note: This means an implicit class cannot be a case class*. |
| 82 | + |
| 83 | + object Bar |
| 84 | + implicit class Bar(x: Int) // BAD! |
| 85 | + |
| 86 | + val x = 5 |
| 87 | + implicit class x(y: Int) // BAD! |
| 88 | + |
| 89 | + implicit case class Baz(x: Int) // BAD! |
| 90 | + |
| 91 | + |
0 commit comments