Skip to content

add support for Scala 2 varargs in scala 3 macro #167

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

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
4 changes: 3 additions & 1 deletion mainargs/src-3/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ object Macros {
def unapply(using Quotes)(tpe: quotes.reflect.TypeRepr): Option[quotes.reflect.TypeRepr] = {
import quotes.reflect.*
tpe match {
case AnnotatedType(AppliedType(_, Seq(arg)), x)
case AnnotatedType(AppliedType(_, Seq(arg)), x) // Scala 3 repeated parameter
if x.tpe =:= defn.RepeatedAnnot.typeRef => Some(arg)
case AppliedType(TypeRef(pre, "<repeated>"), Seq(arg)) // Scala 2 repeated parameter, can be read from Scala 3
if pre =:= defn.ScalaPackage.termRef => Some(arg)
case _ => None
}
}
Expand Down
19 changes: 19 additions & 0 deletions mainargs/test/src-3/VarargsScala2CompatTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package mainargs
import utest._

object VarargsScala2CompatTests extends VarargsBaseTests {
object Base {

// (foo: scala.`<repeated>`[T]) === (foo: T*) in Scala 2 pickles (which can be read from Scala 3)
// in Scala 3, the equivalent is (foo: Seq[T] @repeated)
@main
def pureVariadic(nums: scala.`<repeated>`[Int]) = nums.sum

@main
def mixedVariadic(@arg(short = 'f') first: Int, args: scala.`<repeated>`[String]) =
first.toString + args.mkString
}

val check = new Checker(ParserForMethods(Base), allowPositional = true)
val isNewVarargsTests = false
}