Skip to content

Commit

Permalink
Add upper bounds to Tuple.{Map,FlatMap} args
Browse files Browse the repository at this point in the history
Fixes #12645
  • Loading branch information
nicolasstucki committed Jun 3, 2021
1 parent 1d5a2c4 commit 28a695e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions library/src/scala/Tuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,18 @@ object Tuple {
}

/** Fold a tuple `(T1, ..., Tn)` into `F[T1, F[... F[Tn, Z]...]]]` */
type Fold[T <: Tuple, Z, F[_, _]] = T match
type Fold[Tup <: Tuple, Z, F[_, _]] = Tup match
case EmptyTuple => Z
case h *: t => F[h, Fold[t, Z, F]]

/** Converts a tuple `(T1, ..., Tn)` to `(F[T1], ..., F[Tn])` */
type Map[Tup <: Tuple, F[_]] <: Tuple = Tup match {
type Map[Tup <: Tuple, F[_ <: Union[Tup]]] <: Tuple = Tup match {
case EmptyTuple => EmptyTuple
case h *: t => F[h] *: Map[t, F]
}

/** Converts a tuple `(T1, ..., Tn)` to a flattened `(..F[T1], ..., ..F[Tn])` */
type FlatMap[Tup <: Tuple, F[_] <: Tuple] <: Tuple = Tup match {
type FlatMap[Tup <: Tuple, F[_ <: Union[Tup]] <: Tuple] <: Tuple = Tup match {
case EmptyTuple => EmptyTuple
case h *: t => Concat[F[h], FlatMap[t, F]]
}
Expand Down
11 changes: 11 additions & 0 deletions tests/pos/i12645.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
case class TBox[A <: Tuple](v: A)
case class IBox[A <: Int](v: A)

@main def m =
val t: TBox[EmptyTuple] = TBox(EmptyTuple)
val tt: Tuple.Map[(EmptyTuple, EmptyTuple), TBox] = (TBox(EmptyTuple), TBox(EmptyTuple))

val tt2: Tuple.Map[(1, 2), IBox] = (IBox(1), IBox(2))

type F[X] = (X, X)
val tt3: Tuple.FlatMap[(1, 2), F] = (1, 1, 2, 2)

0 comments on commit 28a695e

Please sign in to comment.