Skip to content

Commit

Permalink
add support for Scala 2 varargs in scala 3 macro (#167)
Browse files Browse the repository at this point in the history
This would allow to extend `ScalafixModule` in Mill with scala 3,
without having to override `fix` command

off this problem goes away if they republish, but it could be nice to
have anyway - as other mixed 2.13 classpaths could be needed by a user
  • Loading branch information
bishabosha authored Oct 11, 2024
1 parent 44e4351 commit 7ed0ce1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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
}

0 comments on commit 7ed0ce1

Please sign in to comment.