You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Welcome to Scala3.2.1 (19, JavaOpenJDK64-BitServerVM).
Type in expressions for evaluation. Ortry:help.
scala>objectX { defunapplySeq(ns: List[Int]):Option[(Int, Seq[Int])] =Some((ns.head, ns.tail)) }
// defined object X
scala>List(1,2,3,4) match { caseX(h, t*) => t case _ =>Nil }
valres0:Seq[Int] =List(2, 3, 4)
scala>List(1,2,3,4) match { caseX(h, m, t*) => t case _ =>Nil }
valres1:Seq[Int] =List(3, 4)
Output
It allows more extracted elements than seems to be defined on the extractor.
This is not allowed on construction:
scala> List(1, 2, List(3, 4): _*)
^
error: Sequence argument type annotation `: _*` cannot be used here:
it is not the only argument to be passed to the single repeated parameter Int*
Expectation
➜ ~ scala -Xlint:stars-align
Welcome to Scala 2.13.10 (OpenJDK 64-Bit Server VM, Java 19).
Type in expressions for evaluation. Or try :help.
scala> object X { def unapplySeq(ns: List[Int]): Option[(Int, Seq[Int])] = Some((ns.head, ns.tail)) }
object X
scala> List(1,2,3,4) match { case X(h, m, t @ _*) => (m, t) case _ => (0, Nil) }
^
warning: Sequence wildcard (_*) does not align with repeated case parameter or extracted sequence; the result may be unexpected.
val res0: (Int, Seq[Int]) = (2,List(3, 4))
The text was updated successfully, but these errors were encountered:
scala> val r = "(a*)(b*)(c*)".r
r: scala.util.matching.Regex = (a*)(b*)(c*)
scala> "abc" match { case r(as, rest @ _*) => as } // dangerous?
res5: String = a
scala> "abc" match { case r(as, _, _) => as }
res6: String = a
Maybe the warnable part is "extractor at odds with constructor", but of course extractors are at liberty to extract whatever.
The OP on the ticket says that the mistake was to define unapplySeq instead of unapply.
Maybe the narrower warning is about "extractor at odds with constructor with trailing sequence".
The regex example is a different issue, whether it should unapply for improved warnings.
Compiler version
3.2.1
Minimized code
Output
It allows more extracted elements than seems to be defined on the extractor.
This is not allowed on construction:
Expectation
The text was updated successfully, but these errors were encountered: