Skip to content

Commit f6fb227

Browse files
committed
Use : instead of as
Following the discussion in #7151, choose `:` instead of `as` for given clauses.
1 parent dbded50 commit f6fb227

File tree

5 files changed

+57
-58
lines changed

5 files changed

+57
-58
lines changed

docs/docs/reference/contextual/delegates.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ trait Ord[T] {
1313
def (x: T) > (y: T) = compare(x, y) > 0
1414
}
1515

16-
given IntOrd as Ord[Int] {
16+
given intOrd: Ord[Int] {
1717
def compare(x: Int, y: Int) =
1818
if (x < y) -1 else if (x > y) +1 else 0
1919
}
2020

21-
given ListOrd[T](given ord: Ord[T]) as Ord[List[T]] {
21+
given listOrd[T](given ord: Ord[T]): Ord[List[T]] {
2222

2323
def compare(xs: List[T], ys: List[T]): Int = (xs, ys) match {
2424
case (Nil, Nil) => 0
@@ -30,19 +30,19 @@ given ListOrd[T](given ord: Ord[T]) as Ord[List[T]] {
3030
}
3131
}
3232
```
33-
This code defines a trait `Ord` with two given instances. `IntOrd` defines
34-
a given for the type `Ord[Int]` whereas `ListOrd[T]` defines givens
33+
This code defines a trait `Ord` with two given instances. `intOrd` defines
34+
a given for the type `Ord[Int]` whereas `listOrd[T]` defines givens
3535
for `Ord[List[T]]` for all types `T` that come with a given instance for `Ord[T]` themselves.
36-
The `given (ord: Ord[T])` clause in `ListOrd` defines an implicit parameter.
36+
The `(given ord: Ord[T])` clause in `listOrd` defines an implicit parameter.
3737
Given clauses are further explained in the [next section](./given-clauses.md).
3838

3939
## Anonymous Given Instances
4040

4141
The name of a given instance can be left out. So the definitions
4242
of the last section can also be expressed like this:
4343
```scala
44-
given as Ord[Int] { ... }
45-
given [T](given Ord[T]) as Ord[List[T]] { ... }
44+
given Ord[Int] { ... }
45+
given [T](given Ord[T]): Ord[List[T]] { ... }
4646
```
4747
If the name of a given is missing, the compiler will synthesize a name from
4848
the type(s) in the `as` clause.
@@ -51,7 +51,7 @@ the type(s) in the `as` clause.
5151

5252
An alias can be used to define a given instance that is equal to some expression. E.g.:
5353
```scala
54-
given global as ExecutionContext = new ForkJoinPool()
54+
given global: ExecutionContext = new ForkJoinPool()
5555
```
5656
This creates a given `global` of type `ExecutionContext` that resolves to the right
5757
hand side `new ForkJoinPool()`.
@@ -60,8 +60,8 @@ returned for this and all subsequent accesses to `global`.
6060

6161
Alias givens can be anonymous, e.g.
6262
```scala
63-
given as Position = enclosingTree.position
64-
given (given outer: Context) as Context = outer.withOwner(currentOwner)
63+
given Position = enclosingTree.position
64+
given (given outer: Context): Context = outer.withOwner(currentOwner)
6565
```
6666
An alias given can have type parameters and given clauses just like any other given instance, but it can only implement a single type.
6767

@@ -78,11 +78,12 @@ Here is the new syntax of given instances, seen as a delta from the [standard co
7878
```
7979
TmplDef ::= ...
8080
| ‘given’ GivenDef
81-
GivenDef ::= [id] [DefTypeParamClause] {GivenParamClause} GivenBody
82-
GivenBody ::= [‘as’ ConstrApp {‘,’ ConstrApp }] [TemplateBody]
83-
| ‘as’ Type ‘=’ Expr
81+
GivenDef ::= GivenBody
82+
| [id] [DefTypeParamClause] {GivenParamClause}
83+
(‘:’ GivenBody | ‘<:’ Type ‘=’ Expr)
84+
GivenBody ::= [ConstrApp {‘,’ ConstrApp }] [TemplateBody]
85+
| Type ‘=’ Expr
8486
GivenParamClause ::= ‘(’ ‘given’ (DefParams | GivenTypes) ‘)’
8587
GivenTypes ::= AnnotType {‘,’ AnnotType}
8688
```
87-
The identifier `id` can be omitted only if either the `as` part or the template body is present.
88-
If the `as` part is missing, the template body must define at least one extension method.
89+
The identifier `id` can be omitted only if some types are implemented or the template body defines at least one extension method.

docs/docs/reference/contextual/import-delegate.md

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ layout: doc-page
33
title: "Import Given"
44
---
55

6-
A special form of import selector is used to import given instances. Example:
6+
A special form of import wildcard selector is used to import given instances. Example:
77
```scala
88
object A {
99
class TC
10-
given tc as TC
11-
def f where TC = ???
10+
given tc: TC
11+
def f(given TC) = ???
1212
}
1313
object B {
1414
import A._
@@ -19,16 +19,16 @@ In the code above, the `import A._` clause of object `B` will import all members
1919
of `A` _except_ the given instance `tc`. Conversely, the second import `import A.given` will import _only_ that given instance.
2020
The two import clauses can also be merged into one:
2121
```scala
22-
object B:
22+
object B
2323
import A.{given, _}
2424
```
2525

26-
Generally, a normal import selector brings definitions other than given instances into scope whereas a `given` import selector brings only given instances into scope.
26+
Generally, a normal wildcard selector `_` brings all definitions other than given instances into scope whereas a `given` selector brings all given instances into scope.
2727

2828
There are two main benefits arising from these rules:
2929

3030
- It is made clearer where givens in scope are coming from.
31-
In particular, it is not possible to hide imported givens in a long list of regular imports.
31+
In particular, it is not possible to hide imported givens in a long list of regular wildcard imports.
3232
- It enables importing all givens
3333
without importing anything else. This is particularly important since givens
3434
can be anonymous, so the usual recourse of using named imports is not
@@ -39,32 +39,32 @@ There are two main benefits arising from these rules:
3939
Since givens can be anonymous it is not always practical to import them by their name, and wildcard imports are typically used instead. By-type imports provide a more specific alternative to wildcard imports, which makes it clearer what is imported. Example:
4040

4141
```scala
42-
import A.{given as TC}
42+
import A.{given TC}
4343
```
4444
This imports any given in `A` that has a type which conforms to `TC`. Importing givens of several types `T1,...,Tn`
4545
is expressed by multiple `given` selectors.
4646
```
47-
import A.{given as T1, ..., given as Tn}
47+
import A.{given T1, ..., given Tn}
4848
```
4949
Importing all given instances of a parameterized type is expressed by wildcard arguments.
5050
For instance, assuming the object
5151
```scala
5252
object Instances {
53-
given intOrd as Ordering[Int]
54-
given [T: Ordering] listOrd as Ordering[List[T]]
55-
given ec as ExecutionContext = ...
56-
given im as Monoid[Int]
53+
given intOrd: Ordering[Int]
54+
given [T: Ordering] listOrd: Ordering[List[T]]
55+
given ec: ExecutionContext = ...
56+
given im: Monoid[Int]
5757
}
5858
```
5959
the import
6060
```scala
61-
import Instances.{given as Ordering[?], given as ExecutionContext}
61+
import Instances.{given Ordering[?], given ExecutionContext}
6262
```
6363
would import the `intOrd`, `listOrd`, and `ec` instances but leave out the `im` instance, since it fits none of the specified bounds.
6464

6565
By-type imports can be mixed with by-name imports. If both are present in an import clause, by-type imports come last. For instance, the import clause
6666
```scala
67-
import Instances.{given im, given as Ordering[?]}
67+
import Instances.{im, given Ordering[?]}
6868
```
6969
would import `im`, `intOrd`, and `listOrd` but leave out `ec`.
7070

@@ -89,15 +89,13 @@ normal imports to given instances and imports.
8989
The following modifications avoid this hurdle to migration.
9090

9191
1. A `given` import selector also brings old style implicits into scope. So, in Scala 3.0
92-
an old-style implicit definition can be brought into scope either by a normal import or by an import given.
92+
an old-style implicit definition can be brought into scope either by a `_` wildcard import or by a `given` import.
9393

94-
2. In Scala 3.1, old-style implicits accessed through a normal import
95-
will give a deprecation warning.
94+
2. In Scala 3.1, old-style implicits accessed through a `_` wildcard import will give a deprecation warning.
9695

97-
3. In some version after 3.1, old-style implicits accessed through a normal import
98-
will give a compiler error.
96+
3. In some version after 3.1, old-style implicits accessed through a `_` wildcard import will give a compiler error.
9997

100-
These rules mean that library users can use `import given` to access old-style implicits in Scala 3.0,
98+
These rules mean that library users can use `given` imports to access old-style implicits in Scala 3.0,
10199
and will be gently nudged and then forced to do so in later versions. Libraries can then switch to
102100
representation clauses once their user base has migrated.
103101

@@ -110,7 +108,7 @@ ImportSpec ::= id
110108
| ‘_’
111109
| ‘given’
112110
| ‘{’ ImportSelectors) ‘}’
113-
ImportSelectors ::= [‘given’] id [‘=>’ id | ‘=>’ ‘_’] [‘,’ ImportSelectors]
111+
ImportSelectors ::= id [‘=>’ id | ‘=>’ ‘_’] [‘,’ ImportSelectors]
114112
| WildCardSelector {‘,’ WildCardSelector}
115113
WildCardSelector ::= ‘given’ [‘as’ InfixType]
116114
| ‘_' [‘:’ InfixType]

docs/docs/reference/contextual/multiversal-equality.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class T derives Eql
3333
```
3434
Alternatively, one can also provide an `Eql` given instance directly, like this:
3535
```scala
36-
given as Eql[T, T] = Eql.derived
36+
given Eql[T, T] = Eql.derived
3737
```
3838
This definition effectively says that values of type `T` can (only) be
3939
compared to other values of type `T` when using `==` or `!=`. The definition
@@ -59,10 +59,10 @@ definitions below make values of type `A` and type `B` comparable with
5959
each other, but not comparable to anything else:
6060

6161
```scala
62-
given as Eql[A, A] = Eql.derived
63-
given as Eql[B, B] = Eql.derived
64-
given as Eql[A, B] = Eql.derived
65-
given as Eql[B, A] = Eql.derived
62+
given Eql[A, A] = Eql.derived
63+
given Eql[B, B] = Eql.derived
64+
given Eql[A, B] = Eql.derived
65+
given Eql[B, A] = Eql.derived
6666
```
6767
The `scala.Eql` object defines a number of `Eql` givens that together
6868
define a rule book for what standard types can be compared (more details below).
@@ -97,7 +97,7 @@ class Box[T](x: T) derives Eql
9797
By the usual rules if [typeclass derivation](./derivation.md),
9898
this generates the following `Eql` instance in the companion object of `Box`:
9999
```scala
100-
given [T, U](given Eql[T, U]) as Eql[Box[T], Box[U]] = Eql.derived
100+
given [T, U](given Eql[T, U]) : Eql[Box[T], Box[U]] = Eql.derived
101101
```
102102
That is, two boxes are comparable with `==` or `!=` if their elements are. Examples:
103103
```scala

docs/docs/reference/contextual/relationship-implicits.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ Given instances can be mapped to combinations of implicit objects, classes and i
1313

1414
1. Given instances without parameters are mapped to implicit objects. E.g.,
1515
```scala
16-
given IntOrd as Ord[Int] { ... }
16+
given intOrd: Ord[Int] { ... }
1717
```
1818
maps to
1919
```scala
2020
implicit object IntOrd extends Ord[Int] { ... }
2121
```
2222
2. Parameterized given instances are mapped to combinations of classes and implicit methods. E.g.,
2323
```scala
24-
given ListOrd[T](given (ord: Ord[T]) as Ord[List[T]] { ... }
24+
given listOrd[T](given (ord: Ord[T]): Ord[List[T]] { ... }
2525
```
2626
maps to
2727
```scala
@@ -35,10 +35,10 @@ Given instances can be mapped to combinations of implicit objects, classes and i
3535
Examples:
3636

3737
```scala
38-
given global as ExecutionContext = new ForkJoinContext()
38+
given global: ExecutionContext = new ForkJoinContext()
3939

4040
val ctx: Context
41-
given as Context = ctx
41+
given Context = ctx
4242
```
4343
would map to
4444
```scala
@@ -50,14 +50,14 @@ Given instances can be mapped to combinations of implicit objects, classes and i
5050

5151
Anonymous given instances get compiler synthesized names, which are generated in a reproducible way from the implemented type(s). For example, if the names of the `IntOrd` and `ListOrd` givens above were left out, the following names would be synthesized instead:
5252
```scala
53-
given Ord_Int_given as Ord[Int] { ... }
54-
given Ord_List_given[T] as Ord[List[T]] { ... }
53+
given given_Ord_Int : Ord[Int] { ... }
54+
given given_Ord_List[T] : Ord[List[T]] { ... }
5555
```
5656
The synthesized type names are formed from
5757

58+
- the prefix `given_`,
5859
- the simple name(s) of the implemented type(s), leaving out any prefixes,
59-
- the simple name(s) of the toplevel argument type constructors to these types
60-
- the suffix `_given`.
60+
- the simple name(s) of the toplevel argument type constructors to these types.
6161

6262
Anonymous given instances that define extension methods without also implementing a type
6363
get their name from the name of the first extension method and the toplevel type
@@ -67,7 +67,7 @@ constructor of its first parameter. For example, the given instance
6767
def (xs: List[T]) second[T] = ...
6868
}
6969
```
70-
gets the synthesized name `second_of_List_T_given`.
70+
gets the synthesized name `given_second_of_List_T`.
7171

7272
### Given Clauses
7373

@@ -134,7 +134,7 @@ Implicit conversion methods in Scala 2 can be expressed as given instances of th
134134
```
135135
one can write
136136
```scala
137-
given stringToToken as Conversion[String, Token] {
137+
given stringToToken: Conversion[String, Token] {
138138
def apply(str: String): Token = new KeyWord(str)
139139
}
140140
```
@@ -153,7 +153,7 @@ E.g., Scala 2's
153153
can be expressed in Dotty as
154154
```scala
155155
lazy val pos: Position = tree.sourcePos
156-
given as Position = pos
156+
given Position = pos
157157
```
158158

159159
### Abstract Implicits
@@ -165,7 +165,7 @@ An abstract implicit `val` or `def` in Scala 2 can be expressed in Dotty using a
165165
can be expressed in Dotty as
166166
```scala
167167
def symDeco: SymDeco
168-
given as SymDeco = symDeco
168+
given SymDeco = symDeco
169169
```
170170

171171
## Implementation Status and Timeline

docs/docs/reference/contextual/typeclasses.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ object Monoid {
2020
def apply[T](given Monoid[T]) = the[Monoid[T]]
2121
}
2222

23-
given as Monoid[String] {
23+
given Monoid[String] {
2424
def (x: String) combine (y: String): String = x.concat(y)
2525
def unit: String = ""
2626
}
2727

28-
given as Monoid[Int] {
28+
given Monoid[Int] {
2929
def (x: Int) combine (y: Int): Int = x + y
3030
def unit: Int = 0
3131
}
@@ -48,14 +48,14 @@ trait Monad[F[_]] extends Functor[F] {
4848
def pure[A](x: A): F[A]
4949
}
5050

51-
given ListMonad as Monad[List] {
51+
given listMonad: Monad[List] {
5252
def (xs: List[A]) flatMap [A, B] (f: A => List[B]): List[B] =
5353
xs.flatMap(f)
5454
def pure[A](x: A): List[A] =
5555
List(x)
5656
}
5757

58-
given ReaderMonad[Ctx] as Monad[[X] =>> Ctx => X] {
58+
given readerMonad[Ctx]: Monad[[X] =>> Ctx => X] {
5959
def (r: Ctx => A) flatMap [A, B] (f: A => Ctx => B): Ctx => B =
6060
ctx => f(r(ctx))(ctx)
6161
def pure[A](x: A): Ctx => A =

0 commit comments

Comments
 (0)