Skip to content

Commit 7ed0ce1

Browse files
authored
add support for Scala 2 varargs in scala 3 macro (#167)
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
1 parent 44e4351 commit 7ed0ce1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

mainargs/src-3/Macros.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ object Macros {
7272
def unapply(using Quotes)(tpe: quotes.reflect.TypeRepr): Option[quotes.reflect.TypeRepr] = {
7373
import quotes.reflect.*
7474
tpe match {
75-
case AnnotatedType(AppliedType(_, Seq(arg)), x)
75+
case AnnotatedType(AppliedType(_, Seq(arg)), x) // Scala 3 repeated parameter
7676
if x.tpe =:= defn.RepeatedAnnot.typeRef => Some(arg)
77+
case AppliedType(TypeRef(pre, "<repeated>"), Seq(arg)) // Scala 2 repeated parameter, can be read from Scala 3
78+
if pre =:= defn.ScalaPackage.termRef => Some(arg)
7779
case _ => None
7880
}
7981
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package mainargs
2+
import utest._
3+
4+
object VarargsScala2CompatTests extends VarargsBaseTests {
5+
object Base {
6+
7+
// (foo: scala.`<repeated>`[T]) === (foo: T*) in Scala 2 pickles (which can be read from Scala 3)
8+
// in Scala 3, the equivalent is (foo: Seq[T] @repeated)
9+
@main
10+
def pureVariadic(nums: scala.`<repeated>`[Int]) = nums.sum
11+
12+
@main
13+
def mixedVariadic(@arg(short = 'f') first: Int, args: scala.`<repeated>`[String]) =
14+
first.toString + args.mkString
15+
}
16+
17+
val check = new Checker(ParserForMethods(Base), allowPositional = true)
18+
val isNewVarargsTests = false
19+
}

0 commit comments

Comments
 (0)