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: Allow '.' in front of extension method
Allow
def (c: Circle).circumference: Double
alongside
def (c: Circle) circumference: Double
The syntax with '.' is preferred for normal methods, which have names
starting with a letter and which are not declared @infix. Right now,
this preference is not enforced.
Copy file name to clipboardExpand all lines: docs/docs/contributing/debugging.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -88,7 +88,7 @@ But you can also do:
88
88
assertPositioned(tree.reporting(s"Tree is: $result"))
89
89
```
90
90
91
-
`def (a: A)reporting(f: given WrappedResult[T] => String, p: Printer = Printers.default): A` is defined on all types. The function `f` can be written without the argument since the argument is `given`. The `result` variable is a part of the `WrapperResult` – a tiny framework powering the `reporting` function. Basically, whenever you are using `reporting` on an object`A`, you can use the `result: A` variable from this function and it will be equal to the objectyou are calling `reporting` on.
91
+
`def (a: A).reporting(f: given WrappedResult[T] => String, p: Printer = Printers.default): A` is defined on all types. The function `f` can be written without the argument since the argument is `given`. The `result` variable is a part of the `WrapperResult` – a tiny framework powering the `reporting` function. Basically, whenever you are using `reporting` on an object`A`, you can use the `result: A` variable from this function and it will be equal to the objectyou are calling `reporting` on.
92
92
93
93
##Printing out trees after phases
94
94
To print out the trees you are compiling after the FrontEnd (scanner, parser, namer, typer) phases:
Like regular methods, extension methods can be invoked with infix `.`:
@@ -42,7 +42,7 @@ As an example, consider an extension method `longestStrings` on `Seq[String]` de
42
42
43
43
```scala
44
44
traitStringSeqOps {
45
-
def (xs: Seq[String])longestStrings = {
45
+
def (xs: Seq[String]).longestStrings = {
46
46
valmaxLength= xs.map(_.length).max
47
47
xs.filter(_.length == maxLength)
48
48
}
@@ -80,22 +80,26 @@ So `circle.circumference` translates to `CircleOps.circumference(circle)`, provi
80
80
81
81
### Operators
82
82
83
-
The extension method syntax also applies to the definition of operators.
84
-
In each case the definition syntax mirrors the way the operator is applied.
83
+
The extension method syntax also applies to the definition of operators.
84
+
In this case it is allowed and preferable to omit the period between the leading parameter list
85
+
and the operator. In each case the definition syntax mirrors the way the operator is applied.
85
86
Examples:
86
87
```scala
87
88
def (x: String) < (y: String) = ...
88
89
def (x: Elem) +: (xs: Seq[Elem]) = ...
90
+
def (x: Number) min (y: Number) = ...
89
91
90
92
"ab"<"c"
91
93
1+:List(2, 3)
94
+
x min 3
92
95
```
93
-
The two definitions above translate to
96
+
The three definitions above translate to
94
97
```scala
95
98
def< (x: String)(y: String) = ...
96
99
def+: (xs: Seq[Elem])(x: Elem) = ...
100
+
defmin(x: Number)(y: Number) = ...
97
101
```
98
-
Note that swap of the two parameters `x` and `xs` when translating
102
+
Note the swap of the two parameters `x` and `xs` when translating
99
103
the right-binding operator `+:` to an extension method. This is analogous
100
104
to the implementation of right binding operators as normal methods.
101
105
@@ -144,7 +148,7 @@ If a given extension is anonymous (as in the last clause), its name is synthesiz
144
148
The extensions above are equivalent to the following regular given instances where the implemented parent is `AnyRef` and the parameters in the `extension` clause are repeated in each extension method definition:
@@ -165,7 +169,7 @@ Here are the syntax changes for extension methods and given extensions relative
165
169
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.
Copy file name to clipboardExpand all lines: docs/docs/reference/dropped-features/package-objects.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ def b = a._2
22
22
caseclassC()
23
23
24
24
implicitobjectCops {
25
-
def (x: C)pair(y: C) = (x, y)
25
+
def (x: C).pair(y: C) = (x, y)
26
26
}
27
27
```
28
28
There may be several source files in a package containing such toplevel definitions, and source files can freely mix toplevel value, method, and type definitions with classes and objects.
`TupledFunction` can be used to generalize the `Function.untupled` methods to functions of any arities ([full example](https://github.com/lampepfl/dotty/blob/master/tests/run/tupled-function-untupled.scala))
`TupledFunction` can also be used to generalize the [`Tuple1.compose`](https://github.com/lampepfl/dotty/blob/master/tests/run/tupled-function-compose.scala) and [`Tuple1.andThen`](https://github.com/lampepfl/dotty/blob/master/tests/run/tupled-function-andThen.scala) methods to compose functions of larger arities and with functions that return tuples.
0 commit comments