Skip to content

Commit 0e5d3bf

Browse files
committed
Convert more conditional given instances to new syntax
1 parent 631b5fe commit 0e5d3bf

File tree

5 files changed

+14
-12
lines changed

5 files changed

+14
-12
lines changed

docs/docs/reference/contextual/delegates.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ given intOrd: Ord[Int] {
1818
if (x < y) -1 else if (x > y) +1 else 0
1919
}
2020

21-
given listOrd[T](given ord: Ord[T]): Ord[List[T]] {
21+
given listOrd[T]: (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
@@ -33,16 +33,18 @@ given listOrd[T](given ord: Ord[T]): Ord[List[T]] {
3333
This code defines a trait `Ord` with two given instances. `intOrd` defines
3434
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.
37-
Given clauses are further explained in the [next section](./given-clauses.md).
36+
The `(ord: Ord[T]) =>` clause in `listOrd` defines a condition: There must be a
37+
given instance of type `Ord[T]` so that a given instance of type `List[Ord[T]]` can
38+
be synthesized. Such conditions are expanded by the compiler to implicit
39+
parameters, which are explained in the [next section](./given-clauses.md).
3840

3941
## Anonymous Given Instances
4042

4143
The name of a given instance can be left out. So the definitions
4244
of the last section can also be expressed like this:
4345
```scala
4446
given Ord[Int] { ... }
45-
given [T](given Ord[T]): Ord[List[T]] { ... }
47+
given [T]: Ord[T] => Ord[List[T]] { ... }
4648
```
4749
If the name of a given is missing, the compiler will synthesize a name from
4850
the implemented type(s).
@@ -61,7 +63,7 @@ returned for this and all subsequent accesses to `global`.
6163
Alias givens can be anonymous, e.g.
6264
```scala
6365
given Position = enclosingTree.position
64-
given (given outer: Context): Context = outer.withOwner(currentOwner)
66+
given (outer: Context) => Context = outer.withOwner(currentOwner)
6567
```
6668
An alias given can have type parameters and given clauses just like any other given instance, but it can only implement a single type.
6769

docs/docs/reference/contextual/derivation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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]): Eq[Opt[T]] =
315+
given derived$Eq[T]: (eqT: Eq[T]) => Eq[Opt[T]] =
316316
eqSum(summon[Mirror[Opt[T]]],
317317
List(
318318
eqProduct(summon[Mirror[Sm[T]]], List(summon[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]): Eq[A] {
332+
given eqSum[A] (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]): Eq[A] {
338+
given eqProduct[A] (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
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ trait Codec[T] {
1212

1313
given intCodec: Codec[Int] = ???
1414

15-
given optionCodec[T](given ev: => Codec[T]): Codec[Option[T]] {
15+
given optionCodec[T]: (ev: => Codec[T]) => Codec[Option[T]] {
1616
def write(xo: Option[T]) = xo match {
1717
case Some(x) => ev.write(x)
1818
case None =>

tests/pos/combine.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ trait Semigroup[A] {
22
def (x: A) combine (y: A): A
33
}
44
given Semigroup[Int] = ???
5-
given [A, B](given Semigroup[A], Semigroup[B]): Semigroup[(A, B)] = ???
5+
given [A, B]: Semigroup[A], Semigroup[B] => Semigroup[(A, B)] = ???
66
object Test extends App {
77
((1, 1)) combine ((2, 2)) // doesn't compile
88
((1, 1): (Int, Int)) combine (2, 2) // compiles

tests/run/implied-specifity-2.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ given Arg
3535

3636
class Bam(val str: String)
3737

38-
given lo(given Low): Bam("lo")
38+
given lo: Low => Bam("lo")
3939

40-
given hi(given High)(given Arg): Bam("hi")
40+
given hi: High => Arg => Bam("hi")
4141

4242
class Bam2(val str: String)
4343

0 commit comments

Comments
 (0)