You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -35,7 +41,7 @@ When is an extension method considered? There are two possibilities. The first (
35
41
36
42
```scala
37
43
implicitobjectStringSeqOps1 {
38
-
deflongestStrings(thisxs: Seq[String]) = {
44
+
def(xs: Seq[String]) longestStrings= {
39
45
valmaxLength= xs.map(_.length).max
40
46
xs.filter(_.length == maxLength)
41
47
}
@@ -49,8 +55,8 @@ is legal everywhere `StringSeqOps1` is available as an implicit value. Alternati
49
55
as a member of a normal object. But then the method has to be brought into scope to be usable as an extension method.
50
56
51
57
```scala
52
-
objectStringSeqOps2{
53
-
deflongestStrings(thisxs: Seq[String]) = {
58
+
objectStringOps2{
59
+
def(xs: Seq[String]) longestStrings= {
54
60
valmaxLength= xs.map(_.length).max
55
61
xs.filter(_.length == maxLength)
56
62
}
@@ -73,11 +79,26 @@ and where `T` is the expected type. The following two rewritings are tried in or
73
79
So `circle.circumference` translates to `CircleOps.circumference(circle)`, provided
74
80
`circle` has type `Circle` and `CircleOps` is an eligible implicit (i.e. it is visible at the point of call or it is defined in the companion object of `Circle`).
75
81
76
-
**Note**: The translation of extension methods is formulated on method calls. It is thus indepenent from the way infix operations are translated to method calls. For instamce,
77
-
if `+:` was formulated as an extension method, it would still have the `this` parameter come first, even though, seen as an operator, `+:` is right-binding:
78
-
```scala
79
-
def+: [T](thisxs: Seq[T])(x: T):Seq[T]
82
+
### Operators
83
+
84
+
The extension method syntax also applies to the definition of operators.
85
+
In each case the definition syntax mirrors the way the operator is applied.
86
+
Examples:
87
+
```
88
+
def (x: String) < (y: String) = ...
89
+
def (x: Elem) +: (xs: Seq[Elem]) = ...
90
+
91
+
"ab" + "c"
92
+
1 +: List(2, 3)
93
+
```
94
+
The two definitions above translate to
95
+
```
96
+
def < (x: String)(y: String) = ...
97
+
def +: (xs: Seq[Elem])(x: Elem) = ...
80
98
```
99
+
Note that swap of the two parameters `x` and `xs` when translating
100
+
the right-binding operator `+:` to an extension method. This is analogous
101
+
to the implementation of right binding operators as normal methods.
81
102
82
103
### Generic Extensions
83
104
@@ -86,7 +107,7 @@ to extend a generic type by adding type parameters to an extension method:
As usual, type parameters of the extension method follow the defined method name. Nevertheless, such type parameters can already be used in the parameter clause that precedes the defined method name.
131
+
As usual, type parameters of the extension method follow the defined method name. Nevertheless, such type parameters can already be used in the preceding parameter clause.
The `ListOrd` class is generic - it works for any type argument `T` that is itself an instance of `Ord`. In current Scala, we could not define `ListOrd` as an implicit class since implicit classes can only define implicit converions that take exactly one non-implicit value parameter. We propose to drop this requirement and to also allow implicit classes without any value parameters, or with only implicit value parameters. The generated implicit method would in each case follow the signature of the class. That is, for `ListOrd` we'd generate the method:
0 commit comments