Skip to content

Commit 631b5fe

Browse files
committed
Tweak syntax of conditions in given instances
Treat given (A, B) => ... as equivalent to given A, B => ... Motivation: Because of the similarity with function types, it's very easy to wrap multiple types in parentheses by mistake. On the other hand, it should be extremely rare to demand a tuple of givens. So the syntax tweak is there to "do what I mean".
1 parent d1579e6 commit 631b5fe

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2787,7 +2787,10 @@ object Parsers {
27872787
var counter = nparams
27882788
def nextIdx = { counter += 1; counter }
27892789
val paramFlags = if ofClass then Private | Local | ParamAccessor else Param
2790-
tps.map(makeSyntheticParameter(nextIdx, _, paramFlags | Synthetic | Given))
2790+
val tps1 = tps match
2791+
case Tuple(tps1) :: Nil => tps1
2792+
case _ => tps
2793+
tps1.map(makeSyntheticParameter(nextIdx, _, paramFlags | Synthetic | Given))
27912794

27922795
/** OLD: GivenTypes ::= AnnotType {‘,’ AnnotType}
27932796
* NEW: GivenTypes ::= Type {‘,’ Type}

tests/pos/tupled-given.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class A
2+
class B
3+
class C
4+
class D
5+
class E
6+
trait F
7+
8+
given A()
9+
given B()
10+
given (A, B) => C() // this one is equivalent to ...
11+
given A, B => D(), F // ... the one without the parens
12+
given ((A, B)) => E() // to demand a tuple, add an extra pair of parens
13+
14+
@main def Test =
15+
summon[C]
16+
summon[D]
17+
summon[F]
18+
given (A, B) = (summon[A], summon[B])
19+
summon[E]

tests/run/poly-kinded-derives.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object Test extends App {
77
given Show[Int] {}
88
given [T]: (st: Show[T]) => Show[Tuple1[T]]
99
given t2[T, U]: (st: Show[T], su: Show[U]) => Show[(T, U)]
10-
given t3 [T, U, V]: (st: Show[T], su: Show[U], sv: Show[V]) => Show[(T, U, V)]
10+
given t3 [T, U, V]: (Show[T], Show[U], Show[V]) => Show[(T, U, V)]
1111

1212
def derived[T](given m: Mirror.Of[T], r: Show[m.MirroredElemTypes]): Show[T] = new Show[T] {}
1313
}

0 commit comments

Comments
 (0)