Skip to content

Commit 1681e2d

Browse files
Merge pull request #8301 from dotty-staging/syntax-changes-doc
Add Syntax Changes document
2 parents 901c5a4 + f202d55 commit 1681e2d

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
layout: doc-page
3+
title: Syntax Changes in Dotty 0.22
4+
---
5+
6+
In 2019, we experimented with several syntax changes in Dotty, most notably in the area of
7+
implicits replacements. In Dotty 0.22, released on Feb 5, 2020, we have settled on what
8+
we believe will be the definite syntax for Scala 3. Dotty 0.23 will support only this
9+
syntax. Previous variants will no longer be supported.
10+
11+
Here is a rundown of how previous variants need to be rewritten in the
12+
new syntax. This will be useful as a migration and learning help for people who have already
13+
written code in one of the previous versions of Dotty.
14+
15+
## Given Instances
16+
17+
Given instances are always written with `given` and `as`. The previous use of `:` instead of `as` is no longer supported. Examples:
18+
```scala
19+
given intOrd as Ordering[Int] { ... }
20+
given [T] as Ordering[List[T]] { ... }
21+
given ctx as ExecutionContext = ...
22+
given Ordering[String] { ... }
23+
```
24+
`as` can be omitted if the instance is anonymous and does not have parameters, as in the last definition above.
25+
26+
## Context Parameters
27+
28+
Context parameters are the replacement of Scala 2's implicit parameters. Context parameters and arguments both start with `using`. Examples:
29+
```scala
30+
def max[T](x: T, y: T)(using Ordering[T]): T = ...
31+
given [T](using Ordering[T]) as Ordering[List[T]] { ... }
32+
33+
max(a, b)(using intOrd)
34+
```
35+
The previous syntax that uses `given` also for context parameters and arguments is no longer supported.
36+
37+
Context bounds remain supported as a shorthand for one-parameter typeclass constraints. So the two definitions above could also be written as
38+
```scala
39+
def max[T: Ordering](x: T, y: T): T = ...
40+
given [T: Ordering] as Ordering[List[T]] { ... }
41+
```
42+
43+
## Context Functions
44+
45+
Implicit function types `implicit A => B` have been replaced with context function types, which are written `A ?=> B`. The syntax `(given A) => B` that was used in earlier Dotty versions is no longer supported.
46+
47+
The same change applies to context function values. It's now
48+
```scala
49+
(x: A) ?=> E
50+
```
51+
instead of `(implicit x: A) => E` or `(given x: A) => E`.
52+
53+
## Given Imports
54+
55+
The syntax of wildcard given import selectors is now `given _`. Examples
56+
```scala
57+
import a.{given _}
58+
import b.{_, given _}
59+
```
60+
The previous syntax, which used just `given` without an underscore is no longer supported. The change was made to better align with typed given import selectors such as `given Ordering[T]`,
61+
which are unchanged.
62+
63+
## Collective Extensions
64+
65+
Collective extensions are now a separate construct. Example:
66+
```scala
67+
extension [T] on List[T] {
68+
def second: T ...
69+
def takeRightWhile(p: T => Boolean): List[T] = ...
70+
}
71+
```
72+
Collective extensions still _expand_ to given instances with regular extension methods, but the previous syntaxes that expressed them as some syntactic variant of given instances are no longer supported.
73+
74+
## Extension Methods
75+
76+
There have been two syntax changes for regular extension methods. First,
77+
any type parameters are now written in front, following the `def`. Second,
78+
a "`.`" in front of the method name is now allowed (but not required). Example:
79+
```scala
80+
def [T](xs: List[T]).second: T
81+
```
82+
The previous syntax which used type parameters after the method name is no longer supported.
83+
84+
## Optional Braces For Definitions
85+
86+
Braces around the definitions of a class, object or similar construct can now be omitted
87+
if the leading signature of the definition is followed by a `:` at the end a line. Examples:
88+
```scala
89+
trait Text:
90+
def toString: String
91+
92+
class Str(str: String) extends Text:
93+
def toString = str
94+
95+
class Append(txt1: Text, txt2: Text) extends Text:
96+
def toString = txt1 ++ txt2
97+
98+
object Empty extends Text:
99+
def toString = ""
100+
101+
extension on (t: Text):
102+
def length = toString.length
103+
104+
given Ordering[Text]:
105+
def compare(txt1: Text, txt2: Text): Int =
106+
summon[Ordering[String]].compare(txt1.toString, txt2.toString)
107+
```
108+
Previous variants required a `with` instead of the `:` or inserted braces around indented code after class, object, ... without any leading token. These are no longer supported.
109+
110+
Note that this interpretation of `:` as an alternative to braces only applies to class-level definitions. The use of `:` at the end of a line to imply braces around a following _function argument_ is not affected by this change. It still requires the `Yindent-colons` option.

0 commit comments

Comments
 (0)