Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Handling varargs in macros #20

Open
anatoliykmetyuk opened this issue Jul 31, 2019 · 2 comments
Open

Handling varargs in macros #20

anatoliykmetyuk opened this issue Jul 31, 2019 · 2 comments
Labels
intent:add-knowledge You have learnt something while working on the Dotty compiler and you want to preserve this info

Comments

@anatoliykmetyuk
Copy link
Contributor

No description provided.

@anatoliykmetyuk anatoliykmetyuk added the intent:add-knowledge You have learnt something while working on the Dotty compiler and you want to preserve this info label Jul 31, 2019
@nicolasstucki
Copy link

nicolasstucki commented Jul 31, 2019

import scala.quoted._
import scala.quoted.matching.{ExprSeq, ConstSeq}

inline def foo(xs: Int*) = ${ impl('xs) }

def impl(xs: Expr[Seq[Int]]) given QuoteContext: Expr[Unit] = xs match {
   case ExprSeq(xs2) => xs2: Seq[Expr[Int]] // example: foo(arg1, arg2, arg3)
  case _ =>  // example: foo(args: _*)
}

def impl2(xs: Expr[Seq[Int]]) given QuoteContext: Expr[Unit] = xs match {
   case ConstSeq(xs2) => xs2: Seq[Int] // example: foo(1, 2, 3)
  case _ =>  // example: foo(args: _*) or foo(arg1, arg2, arg3)
}

@nicolasstucki
Copy link

def f(xs: List[Int]) given QuoteContext: Expr[List[Int]] = 
  '{ List(${xs.toSeq.toExpr}: _*) } // lifted Seq is optimal for varargs

@anatoliykmetyuk anatoliykmetyuk removed their assignment Jul 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
intent:add-knowledge You have learnt something while working on the Dotty compiler and you want to preserve this info
Projects
None yet
Development

No branches or pull requests

2 participants