Skip to content

Commit d068675

Browse files
committed
Update docs
1 parent 931b350 commit d068675

File tree

3 files changed

+11
-193
lines changed

3 files changed

+11
-193
lines changed

docs/docs/reference/contextual/extension-methods-new.md

Lines changed: 0 additions & 180 deletions
This file was deleted.

docs/docs/reference/contextual/extension-methods.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ layout: doc-page
33
title: "Extension Methods"
44
---
55

6-
**Note:** The syntax of extension methods is about to change. Here is the
7-
[doc page with the new syntax](./extension-methods-new.html), supported from Dotty 0.22 onwards.
8-
96
Extension methods allow one to add methods to a type after the type is defined. Example:
107

118
```scala
129
case class Circle(x: Double, y: Double, radius: Double)
1310

14-
def (c: Circle) circumference: Double = c.radius * math.Pi * 2
11+
def (c: Circle).circumference: Double = c.radius * math.Pi * 2
1512
```
1613

1714
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
7269
and where `T` is the expected type. The following two rewritings are tried in order:
7370

7471
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
7774
method named `m`, then selection is expanded to `g.m[Ts](e)`.
7875
This second rewriting is attempted at the time where the compiler also tries an implicit conversion
7976
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
8481
### Operators
8582

8683
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.
8985
Examples:
9086
```scala
9187
def (x: String) < (y: String) = ...
@@ -96,6 +92,8 @@ def (x: Number) min (y: Number) = ...
9692
1 +: List(2, 3)
9793
x min 3
9894
```
95+
For alphanumeric extension operators like `min` an `@infix` annotation is implied.
96+
9997
The three definitions above translate to
10098
```scala
10199
def < (x: String)(y: String) = ...
@@ -106,6 +104,7 @@ Note the swap of the two parameters `x` and `xs` when translating
106104
the right-binding operator `+:` to an extension method. This is analogous
107105
to the implementation of right binding operators as normal methods.
108106

107+
109108
### Generic Extensions
110109

111110
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 {
170169
### Syntax
171170

172171
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.
173+
175174
```
176175
DefSig ::= ...
177176
| ExtParamClause [nl] [‘.’] id DefParamClauses
178177
ExtParamClause ::= [DefTypeParamClause] ‘(’ DefParam ‘)’
179178
TmplDef ::= ...
180179
| ‘extension’ ExtensionDef
181-
ExtensionDef ::= [id] ‘on’ ExtParamClause {GivenParamClause} ‘with’ ExtMethods
180+
ExtensionDef ::= [id] ‘on’ ExtParamClause {GivenParamClause} ExtMethods
182181
ExtMethods ::= ‘{’ ‘def’ DefDef {semi ‘def’ DefDef} ‘}’
183182
```
184-

docs/docs/reference/contextual/typeclasses.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ with canonical implementations defined by given instances. Here are some example
1111

1212
```scala
1313
trait SemiGroup[T] {
14-
@infix def (x: T) combine (y: T): T
14+
def (x: T) combine (y: T): T
1515
}
1616

1717
trait Monoid[T] extends SemiGroup[T] {

0 commit comments

Comments
 (0)