Skip to content

Commit 6ed305b

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

11 files changed

+85
-86
lines changed

docs/docs/reference/contextual/conversions.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ abstract class Conversion[-T, +U] extends (T => U)
1010
```
1111
For example, here is an implicit conversion from `String` to `Token`:
1212
```scala
13-
given as Conversion[String, Token] {
13+
given Conversion[String, Token] {
1414
def apply(str: String): Token = new KeyWord(str)
1515
}
1616
```
1717
Using an alias this can be expressed more concisely as:
1818
```scala
19-
given as Conversion[String, Token] = new KeyWord(_)
19+
given Conversion[String, Token] = new KeyWord(_)
2020
```
2121
An implicit conversion is applied automatically by the compiler in three situations:
2222

@@ -37,7 +37,7 @@ If such an instance `C` is given, the expression `e` is replaced by `C.apply(e)`
3737
primitive number types to subclasses of `java.lang.Number`. For instance, the
3838
conversion from `Int` to `java.lang.Integer` can be defined as follows:
3939
```scala
40-
given int2Integer as Conversion[Int, java.lang.Integer] =
40+
given int2Integer: Conversion[Int, java.lang.Integer] =
4141
java.lang.Integer.valueOf(_)
4242
```
4343

@@ -59,9 +59,9 @@ object Completions {
5959
//
6060
// CompletionArg.fromStatusCode(statusCode)
6161

62-
given fromString as Conversion[String, CompletionArg] = Error(_)
63-
given fromFuture as Conversion[Future[HttpResponse], CompletionArg] = Response(_)
64-
given fromStatusCode as Conversion[Future[StatusCode], CompletionArg] = Status(_)
62+
given fromString : Conversion[String, CompletionArg] = Error(_)
63+
given fromFuture : Conversion[Future[HttpResponse], CompletionArg] = Response(_)
64+
given fromStatusCode : Conversion[Future[StatusCode], CompletionArg] = Status(_)
6565
}
6666
import CompletionArg._
6767

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/derivation.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ The `derives` clause generates the following given instances for the `Eq`, `Orde
1919
companion object of `Tree`,
2020

2121
```scala
22-
given [T: Eq] as Eq[Tree[T]] = Eq.derived
23-
given [T: Ordering] as Ordering[Tree] = Ordering.derived
24-
given [T: Show] as Show[Tree] = Show.derived
22+
given [T: Eq] : Eq[Tree[T]] = Eq.derived
23+
given [T: Ordering] : Ordering[Tree] = Ordering.derived
24+
given [T: Show] : Show[Tree] = Show.derived
2525
```
2626

2727
We say that `Tree` is the _deriving type_ and that the `Eq`, `Ordering` and `Show` instances are _derived instances_.
@@ -175,7 +175,7 @@ we need to implement a method `Eq.derived` on the companion object of `Eq` that
175175
a `Mirror[T]`. Here is a possible implementation,
176176

177177
```scala
178-
inline given derived[T](given m: Mirror.Of[T]) as Eq[T] = {
178+
inline given derived[T](given m: Mirror.Of[T]): Eq[T] = {
179179
val elemInstances = summonAll[m.MirroredElemTypes] // (1)
180180
inline m match { // (2)
181181
case s: Mirror.SumOf[T] => eqSum(s, elemInstances)
@@ -256,7 +256,7 @@ trait Eq[T] {
256256
}
257257

258258
object Eq {
259-
given as Eq[Int] {
259+
given Eq[Int] {
260260
def eqv(x: Int, y: Int) = x == y
261261
}
262262

@@ -281,7 +281,7 @@ object Eq {
281281
}
282282
}
283283

284-
inline given derived[T](given m: Mirror.Of[T]) as Eq[T] = {
284+
inline given derived[T](given m: Mirror.Of[T]): Eq[T] = {
285285
val elemInstances = summonAll[m.MirroredElemTypes]
286286
inline m match {
287287
case s: Mirror.SumOf[T] => eqSum(s, elemInstances)
@@ -312,7 +312,7 @@ In this case the code that is generated by the inline expansion for the derived
312312
following, after a little polishing,
313313

314314
```scala
315-
given derived$Eq[T](given eqT: Eq[T]) as Eq[Opt[T]] =
315+
given derived$Eq[T](given eqT: Eq[T]): Eq[Opt[T]] =
316316
eqSum(the[Mirror[Opt[T]]],
317317
List(
318318
eqProduct(the[Mirror[Sm[T]]], List(the[Eq[T]]))
@@ -329,13 +329,13 @@ As a third example, using a higher level library such as shapeless the type clas
329329
`derived` method as,
330330

331331
```scala
332-
given eqSum[A](given inst: => K0.CoproductInstances[Eq, A]) as Eq[A] {
332+
given eqSum[A](given inst: => K0.CoproductInstances[Eq, A]): Eq[A] {
333333
def eqv(x: A, y: A): Boolean = inst.fold2(x, y)(false)(
334334
[t] => (eqt: Eq[t], t0: t, t1: t) => eqt.eqv(t0, t1)
335335
)
336336
}
337337

338-
given eqProduct[A](given inst: K0.ProductInstances[Eq, A]) as Eq[A] {
338+
given eqProduct[A](given inst: K0.ProductInstances[Eq, A]): Eq[A] {
339339
def eqv(x: A, y: A): Boolean = inst.foldLeft2(x, y)(true: Boolean)(
340340
[t] => (acc: Boolean, eqt: Eq[t], t0: t, t1: t) => Complete(!eqt.eqv(t0, t1))(false)(true)
341341
)
@@ -354,7 +354,7 @@ change the code of the ADT itself. To do this, simply define an instance using
354354
as right-hand side. E.g, to implement `Ordering` for `Option` define,
355355

356356
```scala
357-
given [T: Ordering] as Ordering[Option[T]] = Ordering.derived
357+
given [T: Ordering] : Ordering[Option[T]] = Ordering.derived
358358
```
359359

360360
Assuming the `Ordering.derived` method has a given parameter of type `Mirror[T]` it will be satisfied by the

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ trait StringSeqOps {
5050
```
5151
We can make the extension method available by defining a given `StringSeqOps` instance, like this:
5252
```scala
53-
given ops1 as StringSeqOps
53+
given ops1: StringSeqOps
5454
```
5555
Then
5656
```scala

docs/docs/reference/contextual/given-clauses.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ def f(u: Universe)(given c: u.Context)(given s: ctx.Symbol, k: ctx.Kind) = ...
7474
Multiple given clauses are matched left-to-right in applications. Example:
7575
```scala
7676
object global extends Universe { type Context = ... }
77-
given ctx as global.Context { type Symbol = ...; type Kind = ... }
78-
given sym as ctx.Symbol
79-
given kind as ctx.Kind
77+
given ctx : global.Context { type Symbol = ...; type Kind = ... }
78+
given sym : ctx.Symbol
79+
given kind : ctx.Kind
8080
```
8181
Then the following calls are all valid (and normalize to the last one)
8282
```scala

docs/docs/reference/contextual/implicit-by-name-parameters.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ trait Codec[T] {
1010
def write(x: T): Unit
1111
}
1212

13-
given intCodec as Codec[Int] = ???
13+
given intCodec: Codec[Int] = ???
1414

15-
given optionCodec[T](given ev: => Codec[T]) as Codec[Option[T]] {
15+
given optionCodec[T](given ev: => Codec[T]): Codec[Option[T]] {
1616
def write(xo: Option[T]) = xo match {
1717
case Some(x) => ev.write(x)
1818
case None =>
@@ -36,7 +36,7 @@ The precise steps for synthesizing an argument for an implicit by-name parameter
3636
1. Create a new given instance of type `T`:
3737

3838
```scala
39-
given lv as T = ???
39+
given lv: T = ???
4040
```
4141
where `lv` is an arbitrary fresh name.
4242

@@ -46,7 +46,7 @@ The precise steps for synthesizing an argument for an implicit by-name parameter
4646

4747

4848
```scala
49-
{ given lv as T = E; lv }
49+
{ given lv: T = E; lv }
5050
```
5151

5252
Otherwise, return `E` unchanged.

docs/docs/reference/contextual/implicit-function-types.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type Executable[T] = (given ExecutionContext) => T
1212
An implicit function is applied to synthesized arguments, in
1313
the same way a method with a given clause is applied. For instance:
1414
```scala
15-
given ec as ExecutionContext = ...
15+
given ec: ExecutionContext = ...
1616

1717
def f(x: Int): Executable[Int] = ...
1818

@@ -83,13 +83,13 @@ with implicit function types as parameters to avoid the plumbing boilerplate
8383
that would otherwise be necessary.
8484
```scala
8585
def table(init: (given Table) => Unit) = {
86-
given t as Table
86+
given t: Table
8787
init
8888
t
8989
}
9090

9191
def row(init: (given Row) => Unit)(given t: Table) = {
92-
given r as Row
92+
given r: Row
9393
init
9494
t.add(r)
9595
}

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

Lines changed: 20 additions & 22 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,9 +108,9 @@ ImportSpec ::= id
110108
| ‘_’
111109
| ‘given’
112110
| ‘{’ ImportSelectors) ‘}’
113-
ImportSelectors ::= [‘given’] id [‘=>’ id | ‘=>’ ‘_’] [‘,’ ImportSelectors]
111+
ImportSelectors ::= id [‘=>’ id | ‘=>’ ‘_’] [‘,’ ImportSelectors]
114112
| WildCardSelector {‘,’ WildCardSelector}
115-
WildCardSelector ::= ‘given’ [‘as’ InfixType]
113+
WildCardSelector ::= ‘given’ [InfixType]
116114
| ‘_' [‘:’ InfixType]
117115
Export ::= ‘export’ ImportExpr {‘,’ ImportExpr}
118116
```

0 commit comments

Comments
 (0)