Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add summonAll and constValueTuple #9209

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions library/src/scala/Tuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ object Tuple {
}
}

/**
* 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
43 changes: 43 additions & 0 deletions library/src/scala/compiletime/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import scala.quoted._

package object compiletime {

/** Use this method when you have a type, do not have a value for it but want to
* pattern match on it. For example, given a type `Tup <: Tuple`, one can
* pattern-match on it as follows:
* ```
* erasedValue[Tup] match {
* case _: EmptyTuple => ...
* case _: h *: t => ...
* }
* ```
*/
erased def erasedValue[T]: T = ???

/** The error method is used to produce user-defined compile errors during inline expansion.
Expand Down Expand Up @@ -38,10 +48,29 @@ package object compiletime {
transparent inline def (inline self: StringContext) code (inline args: Any*): String =
${ dotty.internal.CompileTimeMacros.codeExpr('self, 'args) }

/** Same as `constValue` but returns a `None` if a constant value
* cannot be constructed from the provided type. Otherwise returns
* that value wrapped in `Some`.
*/
inline def constValueOpt[T]: Option[T] = ???

/** Given a constant, singleton type `T`, convert it to a value
* of the same singleton type. For example: `assert(constValue[1] == 1)`.
*/
inline def constValue[T]: T = ???

/** Given a tuple type `(X1, ..., Xn)`, returns a tuple value
* `(constValue[X1], ..., constValue[Xn])`.
*/
inline def constValueTuple[T <: Tuple]: Tuple.Widen[T]=
anatoliykmetyuk marked this conversation as resolved.
Show resolved Hide resolved
val res =
inline erasedValue[T] match
case _: EmptyTuple => EmptyTuple
case _: (t *: ts) => constValue[t] *: constValueTuple[ts]
end match
res.asInstanceOf[Tuple.Widen[T]]
end constValueTuple

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

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