Skip to content

Turning inlined varargs into a generic tuple #12651

Discussion options

You must be logged in to vote
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)

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@nicolasstucki
Comment options

@soronpo
Comment options

soronpo May 31, 2021
Collaborator Author

@nicolasstucki
Comment options

Answer selected by julienrf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants