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
The most general form of leading parameters of an extension method is as follows:
8
-
7
+
The most general signature an extension method can have is as follows:
8
+
- An optional type clause `leadingTyParamss`
9
9
- A possibly empty list of using clauses `leadingUsing`
10
-
- A single parameter `extensionParam`
10
+
- A single parameter `leftParamss`
11
11
- A possibly empty list of using clauses `trailingUsing`
12
+
- A name (preceded by the `def` keyword)
13
+
- An optional type clause `rightTyParamss`
14
+
- An optional explicit term clause `rightParamss`
15
+
- Any number of any clauses `rest`
12
16
13
-
This is then followed by `def`, the method name, and possibly further parameters
14
-
`otherParams`. An example is:
17
+
For example:
15
18
16
19
```scala
17
-
extension (usinga: A, b: B)(usingc: C) // <-- leadingUsing
18
-
(x: X) // <-- extensionParam
20
+
extension [T] // <-- leadingTyParamss
21
+
(usinga: A, b: B)(usingc: C) // <-- leadingUsing
22
+
(x: X) // <-- leftParamss
19
23
(usingd: D) // <-- trailingUsing
20
-
def+:: (y: Y)(usinge: E)(z: Z) // <-- otherParams
24
+
def+:: [U] // <-- rightTyParamss
25
+
(y: Y) // <-- rightParamss
26
+
[V](usinge: E)[W](z: Z) // <-- rest
21
27
```
22
28
29
+
23
30
An extension method is treated as a right-associative operator
24
31
(as in [SLS §6.12.3](https://www.scala-lang.org/files/archive/spec/2.13/06-expressions.html#infix-operations))
25
-
if it has a name ending in `:` and is immediately followed by a
26
-
single parameter. In the example above, that parameter is `(y: Y)`.
32
+
if it has a name ending in `:`, and is immediately followed by a
33
+
single explicit term parameter (in other words, `rightParamss` is present). In the example above, that parameter is `(y: Y)`.
27
34
28
35
The Scala compiler pre-processes a right-associative infix operation such as `x +: xs`
29
36
to `xs.+:(x)` if `x` is a pure expression or a call-by-name parameter and to `val y = x; xs.+:(y)` otherwise. This is necessary since a regular right-associative infix method
30
37
is defined in the class of its right operand. To make up for this swap,
31
-
the expansion of right-associative extension methods performs an analogous parameter swap. More precisely, if `otherParams` consists of a single parameter
32
-
`rightParam` followed by `remaining`, the total parameter sequence
38
+
the expansion of right-associative extension methods performs the inverse parameter swap. More precisely, if `rightParamss` is present, the total parameter sequence
0 commit comments