Turning inlined varargs into a generic tuple #12651
Answered
by
nicolasstucki
soronpo
asked this question in
Metaprogramming
Replies: 1 comment 3 replies
-
import scala.quoted.*
def toTupleExpr(argsExpr: Expr[Seq[Int]])(using Quotes): Expr[Tuple] = {
argsExpr match
case Varargs(argExprs) =>
argExprs.foldRight[Expr[Tuple]]('{EmptyTuple}) { (expr, acc) =>
(expr, acc) match // match to extract precise type
case ('{ $expr: head }, '{ type tail <: Tuple; $acc: `tail` }) =>
'{ $expr *: $acc }
}
case _ =>
quotes.reflect.report.throwError(
"Expected explicit argument list but got: " + argsExpr.show, argsExpr)
} transparent inline def toTuple(inline args : Int*) : Tuple =
${ toTupleExpr('args) }
val x : (1, 2, 3) = toTuple(1,2,3) |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
julienrf
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I've tried to do this, but I'm hitting a wall. How can I convert varargs to a tuple generically, without loosing the type narrowness.
E.g.:
Beta Was this translation helpful? Give feedback.
All reactions