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
Like regular methods, extension methods can be invoked with infix `.`:
@@ -72,8 +69,8 @@ Assume a selection `e.m[Ts]` where `m` is not a member of `e`, where the type ar
72
69
and where `T` is the expected type. The following two rewritings are tried in order:
73
70
74
71
1. The selection is rewritten to `m[Ts](e)`.
75
-
2. If the first rewriting does not typecheck with expected type `T`, and there is a given `g`
76
-
in either the current scope or in the context scope of `T` such that`g` defines an extension
72
+
2. If the first rewriting does not typecheck with expected type `T`, and there is a given instance `g`
73
+
in either the current scope or in the context scope of `T`, and`g` defines an extension
77
74
method named `m`, then selection is expanded to `g.m[Ts](e)`.
78
75
This second rewriting is attempted at the time where the compiler also tries an implicit conversion
79
76
from `T` to a type containing `m`. If there is more than one way of rewriting, an ambiguity error results.
@@ -84,8 +81,7 @@ So `circle.circumference` translates to `CircleOps.circumference(circle)`, provi
84
81
### Operators
85
82
86
83
The extension method syntax also applies to the definition of operators.
87
-
In this case it is allowed and preferable to omit the period between the leading parameter list
88
-
and the operator. In each case the definition syntax mirrors the way the operator is applied.
84
+
This case is indicated by omitting the period between the leading parameter list and the operator. In each case the definition syntax mirrors the way the operator is applied.
For alphanumeric extension operators like `min` an `@infix` annotation is implied.
96
+
99
97
The three definitions above translate to
100
98
```scala
101
99
def< (x: String)(y: String) = ...
@@ -106,6 +104,7 @@ Note the swap of the two parameters `x` and `xs` when translating
106
104
the right-binding operator `+:` to an extension method. This is analogous
107
105
to the implementation of right binding operators as normal methods.
108
106
107
+
109
108
### Generic Extensions
110
109
111
110
The `StringSeqOps` examples extended a specific instance of a generic type. It is also possible to extend a generic type by adding type parameters to an extension method. Examples:
@@ -170,15 +169,14 @@ given extension_largest_List_T as AnyRef {
170
169
### Syntax
171
170
172
171
Here are the syntax changes for extension methods and collective extensions relative
173
-
to the [current syntax](../../internals/syntax.md). `extension` is a soft keyword, recognized only
174
-
in tandem with `on`. It can be used as an identifier everywhere else.
172
+
to the [current syntax](../../internals/syntax.md). `extension` is a soft keyword, recognized only in tandem with `on`. It can be used as an identifier everywhere else.
0 commit comments