Skip to content

Commit eeb2e52

Browse files
Add summonAll and constValueTuple
1 parent de75713 commit eeb2e52

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

library/src/scala/compiletime/package.scala

+23
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ package object compiletime {
4242

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

45+
inline def constValueTuple[T <: Tuple]: T =
46+
val res =
47+
inline erasedValue[T] match
48+
case _: EmptyTuple => EmptyTuple
49+
case _: (t *: ts) => constValue[t] *: constValueTuple[ts]
50+
end match
51+
res.asInstanceOf[T]
52+
end constValueTuple
53+
4554
/** Summons first given matching one of the listed cases. E.g. in
4655
*
4756
* given B { ... }
@@ -68,6 +77,20 @@ package object compiletime {
6877
case t: T => t
6978
}
7079

80+
/** Given a tuple T, summons each of its member types and returns them in
81+
* a List.
82+
*
83+
* @tparam T the tuple containing the types of the values to be summoned
84+
* @return the given values typed as elements of the tuple
85+
*/
86+
inline def summonAll[T <: Tuple]: T =
87+
val res =
88+
inline erasedValue[T] match
89+
case _: EmptyTuple => EmptyTuple
90+
case _: (t *: ts) => summonInline[t] *: summonAll[ts]
91+
end match
92+
res.asInstanceOf[T]
93+
end summonAll
7194

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

tests/run/constValueTuple.check

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(foo,bar,10,2.5)

tests/run/constValueTuple.scala

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import compiletime.constValueTuple
2+
3+
@main def Test =
4+
println(constValueTuple["foo" *: "bar" *: 10 *: 2.5 *: EmptyTuple])

tests/run/summonAll.check

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(10,foo,1.2)

tests/run/summonAll.scala

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import compiletime.summonAll
2+
3+
@main def Test =
4+
given as Int = 10
5+
given as String = "foo"
6+
given as Double = 1.2
7+
println(summonAll[Int *: String *: Double *: EmptyTuple])

0 commit comments

Comments
 (0)