Skip to content

Commit

Permalink
Add Widen type to the Tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
anatoliykmetyuk committed Jun 24, 2020
1 parent 3167524 commit 635df4c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 12 additions & 0 deletions library/src/scala/Tuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ object Tuple {
case h *: t => Concat[F[h], FlatMap[t, F]]
}

/**
* Use this type to widen a self-type to a tuple. E.g.
* ```
* val x: (1, 3) = (1, 3)
* val y: Widen[x.type] = x
* ```
*/
type Widen[Tup <: Tuple] <: Tuple = Tup match {
case EmptyTuple => EmptyTuple
case h *: t => h *: t
}

/** Given two tuples, `A1 *: ... *: An * At` and `B1 *: ... *: Bn *: Bt`
* where at least one of `At` or `Bt` is `EmptyTuple` or `Tuple`,
* returns the tuple type `(A1, B1) *: ... *: (An, Bn) *: Ct`
Expand Down
8 changes: 4 additions & 4 deletions library/src/scala/compiletime/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ package object compiletime {

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

inline def constValueTuple[T <: Tuple]: T =
inline def constValueTuple[T <: Tuple]: Tuple.Widen[T]=
val res =
inline erasedValue[T] match
case _: EmptyTuple => EmptyTuple
case _: (t *: ts) => constValue[t] *: constValueTuple[ts]
end match
res.asInstanceOf[T]
res.asInstanceOf[Tuple.Widen[T]]
end constValueTuple

/** Summons first given matching one of the listed cases. E.g. in
Expand Down Expand Up @@ -83,13 +83,13 @@ package object compiletime {
* @tparam T the tuple containing the types of the values to be summoned
* @return the given values typed as elements of the tuple
*/
inline def summonAll[T <: Tuple]: T =
inline def summonAll[T <: Tuple]: Tuple.Widen[T] =
val res =
inline erasedValue[T] match
case _: EmptyTuple => EmptyTuple
case _: (t *: ts) => summonInline[t] *: summonAll[ts]
end match
res.asInstanceOf[T]
res.asInstanceOf[Tuple.Widen[T]]
end summonAll

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

0 comments on commit 635df4c

Please sign in to comment.