|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +type: sip |
| 4 | +title: SIP-13 - Implicit classes |
| 5 | +--- |
| 6 | + |
| 7 | +This SIP is based on [this pre-draft](https://docs.google.com/document/d/1k-aGAGmbrDB-2pJ3uDPpHVKno6p-XbnkVHDc07zPrzQ/edit?hl=en_US). |
| 8 | + |
| 9 | +Material adapted from [http://jorgeortiz85.github.com/ImplicitClassSIP.xhtml](http://jorgeortiz85.github.com/ImplicitClassSIP.xhtml) which is Copyright © 2009, Jorge Ortiz and David Hall |
| 10 | + |
| 11 | +## Abstract ## |
| 12 | + |
| 13 | +A new language construct is proposed to simplify the creation of classes which provide _extension methods_ to another type. |
| 14 | + |
| 15 | +## Description ## |
| 16 | + |
| 17 | +The `implicit` keyword will now be allowed as an annotation on classes. Classes annotated with the `implicit` keyword are refered to as _implicit classes_. |
| 18 | + |
| 19 | +An implicit class must have a primary constructor with *exactly* one argument in its first parameter list. It may also include an additional implicit parameter list. An implicit class must be defined in a scope where method definitions are allowed (not at the top level). An implicit class is desugared into a class and implicit method pairing, where the implciit method mimics the constructor of the class. |
| 20 | + |
| 21 | +The generated implicit method will have the same name as the implicit class. This allows importing the implicit conversion using the name of the class, as one expects from other implicit definitions. |
| 22 | +For example, a definition of the form: |
| 23 | + |
| 24 | +{% highlight scala %} |
| 25 | +implicit class RichInt(n: Int) extends Ordered[Int] { |
| 26 | + def min(m: Int): Int = if (n <= m) n else m |
| 27 | + ... |
| 28 | +} |
| 29 | +{% endhighlight %} |
| 30 | + |
| 31 | +will be transformed by the compiler as follows: |
| 32 | + |
| 33 | +{% highlight scala %} |
| 34 | +class RichInt(n: Int) extends Ordered[Int] { |
| 35 | + def min(m: Int): Int = if (n <= m) n else m |
| 36 | + ... |
| 37 | +} |
| 38 | +implicit final def RichInt(n: Int): RichInt = new RichInt(n) |
| 39 | +{% endhighlight %} |
| 40 | + |
| 41 | +Annotations on `implicit` classes default to attaching to the generated class *and* the method. For example, |
| 42 | + |
| 43 | +{% highlight scala %} |
| 44 | +@bar |
| 45 | +implicit class Foo(n: Int) |
| 46 | +{% endhighlight %} |
| 47 | + |
| 48 | +will desugar into: |
| 49 | + |
| 50 | +{% highlight scala %} |
| 51 | +@bar implicit def Foo(n: Int): Foo = new Foo(n) |
| 52 | +@bar class Foo(n:Int) |
| 53 | +{% endhighlight %} |
| 54 | + |
| 55 | +The `annotation.target` annotations will be expanded to include a `genClass` and `method` annotation. This can be used to target annotations at just the generated class or the generated method of an implicit class. For example: |
| 56 | + |
| 57 | +{% highlight scala %} |
| 58 | +@(bar @genClass) implicit class Foo(n: Int) |
| 59 | +{% endhighlight %} |
| 60 | + |
| 61 | +will desugar into |
| 62 | + |
| 63 | +{% highlight scala %} |
| 64 | +implicit def Foo(n: Int): Foo = new Foo(n) |
| 65 | +@bar class Foo(n: Int) |
| 66 | +{% endhighlight %} |
| 67 | + |
| 68 | + |
| 69 | +## Specification ## |
| 70 | + |
| 71 | +No changes are required to Scala's syntax specification, as the relevant production rules already allow for implicit classes. |
| 72 | + |
| 73 | + LocalModifier ::= ‘implicit’ |
| 74 | + BlockStat ::= {LocalModifier} TmplDef |
| 75 | + TmplDef ::= [‘case’] ‘class’ ClassDef |
| 76 | + |
| 77 | +The language specification (SLS 7.1) would be modified to allow the use of the implicit modifier for classes. A new section on Implicit Classes would describe the behavior of the construct. |
| 78 | + |
| 79 | +## Consequences ## |
| 80 | + |
| 81 | +The new syntax should not break existing code, and so remain source compatible with existing techniques. |
| 82 | + |
| 83 | + |
0 commit comments