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
Syntax change: "extension on" instead of "extension of"
"extension on" is more precise. The thing after the "of/on" is a parameter.
The definition is not an extension "of" this parameter (what does that even
mean?), but rather an extension of the parameter's underlying type.
Note that Dart also uses "extension on".
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual/extension-methods-new.md
+7-6Lines changed: 7 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -128,19 +128,19 @@ A collective extension defines one or more concrete methods that have the same t
128
128
and prefix parameter. Examples:
129
129
130
130
```scala
131
-
extension stringOps of (xs: Seq[String]) {
131
+
extension stringOps on (xs: Seq[String]) {
132
132
deflongestStrings:Seq[String] = {
133
133
valmaxLength= xs.map(_.length).max
134
134
xs.filter(_.length == maxLength)
135
135
}
136
136
}
137
137
138
-
extension listOps of [T](xs: List[T]) {
138
+
extension listOps on [T](xs: List[T]) {
139
139
defsecond= xs.tail.head
140
140
defthird:T= xs.tail.tail.head
141
141
}
142
142
143
-
extension of [T](xs: List[T])(givenOrdering[T]) {
143
+
extension on [T](xs: List[T])(givenOrdering[T]) {
144
144
deflargest(n: Int) = xs.sorted.takeRight(n)
145
145
}
146
146
```
@@ -164,18 +164,19 @@ given extension_largest_List_T: AnyRef {
164
164
}
165
165
```
166
166
167
-
`extension` and `of` are soft keywords. They can also be used as a regular identifiers.
167
+
`extension` and `on` are soft keywords. They can also be used as a regular identifiers.
168
168
169
169
### Syntax
170
170
171
171
Here are the syntax changes for extension methods and given extensions relative
172
-
to the [current syntax](../../internals/syntax.md). `extension` is a soft keyword, recognized only after a `given`. 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