Skip to content

Commit 470c1e0

Browse files
Add section about extension methods type parameters
1 parent 7e76269 commit 470c1e0

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

docs/blog/_posts/2021-02-18-scala3-rc1.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,34 @@ This release brings some last-minute polishings, clean-ups and changes before th
1313
Overall, more than [400 PRs](https://github.com/lampepfl/dotty/pulls?q=is%3Apr+is%3Aclosed+closed%3A%3E2020-12-02+sort%3Acomments-desc) were merged after the M3 release and until today! Read more below!
1414

1515
<!--more-->
16+
# Allow leading context parameters in extension methods
17+
Type parameters on extensions can now be combined with type parameters on the methods themselves. E.g.:
18+
19+
```scala
20+
List(1, 2, 3).second[Int]
21+
extension [T](xs: List[T])
22+
def def sumBy[B](f: A => B)(using Numeric[B]): B = ...
23+
```
24+
25+
Type arguments matching method type parameters are passed as usual:
26+
27+
```scala
28+
List("a", "bb", "ccc").sumBy[Int](_.length)
29+
```
30+
By contrast, type arguments matching type parameters following `extension` can be passed
31+
only if the method is referenced as a regular method:
32+
33+
```scala
34+
List[String]("a", "bb", "ccc").sumBy(_.length)
35+
```
36+
37+
Or, when passing both type arguments:
38+
39+
```scala
40+
List[String]("a", "bb", "ccc").sumBy[Int](_.length)
41+
```
42+
43+
For discussion, see [PR #10940](https://github.com/lampepfl/dotty/pull/10940). For more information about the extension methods, see [documentation](https://dotty.epfl.ch/docs/reference/contextual/extension-methods.html).
1644

1745
# New `import` syntax
1846
The following are the changes to the `import` syntax made in this release.

0 commit comments

Comments
 (0)