Skip to content

Commit

Permalink
Add summonAll and constValueTuple
Browse files Browse the repository at this point in the history
  • Loading branch information
anatoliykmetyuk committed Jul 2, 2020
1 parent 3410426 commit 3aa00b1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
23 changes: 23 additions & 0 deletions library/src/scala/compiletime/package.scala
Expand Up @@ -42,6 +42,15 @@ package object compiletime {

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

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

/** Summons first given matching one of the listed cases. E.g. in
*
* given B { ... }
Expand All @@ -68,6 +77,20 @@ package object compiletime {
case t: T => t
}

/** Given a tuple T, summons each of its member types and returns them in
* a List.
*
* @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 =
val res =
inline erasedValue[T] match
case _: EmptyTuple => EmptyTuple
case _: (t *: ts) => summonInline[t] *: summonAll[ts]
end match
res.asInstanceOf[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
1 change: 1 addition & 0 deletions tests/run/constValueTuple.check
@@ -0,0 +1 @@
(foo,bar,10,2.5)
4 changes: 4 additions & 0 deletions tests/run/constValueTuple.scala
@@ -0,0 +1,4 @@
import compiletime.constValueTuple

@main def Test =
println(constValueTuple["foo" *: "bar" *: 10 *: 2.5 *: EmptyTuple])
1 change: 1 addition & 0 deletions tests/run/summonAll.check
@@ -0,0 +1 @@
(10,foo,1.2)
7 changes: 7 additions & 0 deletions tests/run/summonAll.scala
@@ -0,0 +1,7 @@
import compiletime.summonAll

@main def Test =
given as Int = 10
given as String = "foo"
given as Double = 1.2
println(summonAll[Int *: String *: Double *: EmptyTuple])

0 comments on commit 3aa00b1

Please sign in to comment.