Skip to content

Commit 635df4c

Browse files
Add Widen type to the Tuple
1 parent 3167524 commit 635df4c

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

library/src/scala/Tuple.scala

+12
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ object Tuple {
120120
case h *: t => Concat[F[h], FlatMap[t, F]]
121121
}
122122

123+
/**
124+
* Use this type to widen a self-type to a tuple. E.g.
125+
* ```
126+
* val x: (1, 3) = (1, 3)
127+
* val y: Widen[x.type] = x
128+
* ```
129+
*/
130+
type Widen[Tup <: Tuple] <: Tuple = Tup match {
131+
case EmptyTuple => EmptyTuple
132+
case h *: t => h *: t
133+
}
134+
123135
/** Given two tuples, `A1 *: ... *: An * At` and `B1 *: ... *: Bn *: Bt`
124136
* where at least one of `At` or `Bt` is `EmptyTuple` or `Tuple`,
125137
* returns the tuple type `(A1, B1) *: ... *: (An, Bn) *: Ct`

library/src/scala/compiletime/package.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ package object compiletime {
4242

4343
inline def constValue[T]: T = ???
4444

45-
inline def constValueTuple[T <: Tuple]: T =
45+
inline def constValueTuple[T <: Tuple]: Tuple.Widen[T]=
4646
val res =
4747
inline erasedValue[T] match
4848
case _: EmptyTuple => EmptyTuple
4949
case _: (t *: ts) => constValue[t] *: constValueTuple[ts]
5050
end match
51-
res.asInstanceOf[T]
51+
res.asInstanceOf[Tuple.Widen[T]]
5252
end constValueTuple
5353

5454
/** Summons first given matching one of the listed cases. E.g. in
@@ -83,13 +83,13 @@ package object compiletime {
8383
* @tparam T the tuple containing the types of the values to be summoned
8484
* @return the given values typed as elements of the tuple
8585
*/
86-
inline def summonAll[T <: Tuple]: T =
86+
inline def summonAll[T <: Tuple]: Tuple.Widen[T] =
8787
val res =
8888
inline erasedValue[T] match
8989
case _: EmptyTuple => EmptyTuple
9090
case _: (t *: ts) => summonInline[t] *: summonAll[ts]
9191
end match
92-
res.asInstanceOf[T]
92+
res.asInstanceOf[Tuple.Widen[T]]
9393
end summonAll
9494

9595
/** Succesor of a natural number where zero is the type 0 and successors are reduced as if the definition was

0 commit comments

Comments
 (0)