- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Bug in pattern matcher with list and custom extractor?
Mon, 2011-12-12, 05:05
This comes from stackoverflow:
http://stackoverflow.com/questions/8469139/match-order-with-an-extractor
object :+ {
def unapply[A](l: List[A]): Option[(List[A], A)] = {
if (l.isEmpty)
None
else
Some(l.init, l.last)
}
}
The following will match as "bad"...
List(1, 2, 3) match {
case List(7) => "never"
case init :+ last => "good"
case head :: tail => "bad"
}
It seems that in the explicitouter phase, there is an instance test
for List but that the pattern matcher considers the init :+ last
pattern as matching something other than list:
if (temp6.isInstanceOf[::[Int]]())
{
val temp7: ::[Int] = temp6.asInstanceOf[::
[Int]]();
if (temp7.ne(null).&&(immutable.this.Nil.==(temp7.tl
$1())))
{
"never"
}
else
{
"bad"
}
}
else
{
if ($colon$plus.unapply[Int](temp6).isEmpty().unary_!
())
{
"good"
}
else
throw new MatchError(temp6)
}
Should I submit a bug report?
Mon, 2011-12-12, 15:01
#2
Re: Bug in pattern matcher with list and custom extractor?
Great. Glad I asked before submitting any bug report.
--Jean-Laurent
On Dec 12, 2:08 am, Adriaan Moors wrote:
> thanks for reporting! this is a known bug, though
> (https://issues.scala-lang.org/browse/SI-2337)
>
> fwiw, the virtualizing pattern matcher
> (http://github.com/adriaanm/scala/tree/topic/virtpatmat) does not suffer
> from it:
>
> scala> [adriaan@lampmac13 ~]$ qs *-Yvirtpatmat*
> Welcome to Scala version 2.10.0.rdev-1360-gd8d33ed-b20111209170334 (Java
> HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).
> Type in expressions to have them evaluated.
> Type :help for more information.
>
> scala> object :+ {
> | def unapply[A](l: List[A]): Option[(List[A], A)] = {
> | if (l.isEmpty)
> | None
> | else
> | Some(l.init, l.last)
> | }
> | }
> defined module $colon$plus
>
> scala> List(1, 2, 3) match {
> | case List(7) => "never"
> | case init :+ last => "good"
> | case head :: tail => "bad"
> | }
> warning: there were 1 unchecked warnings; re-run with -unchecked for details
> res0: String = good
fwiw, the virtualizing pattern matcher (http://github.com/adriaanm/scala/tree/topic/virtpatmat) does not suffer from it:
scala> [adriaan@lampmac13 ~]$ qs -YvirtpatmatWelcome to Scala version 2.10.0.rdev-1360-gd8d33ed-b20111209170334 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).Type in expressions to have them evaluated.Type :help for more information.
scala> object :+ { | def unapply[A](l: List[A]): Option[(List[A], A)] = { | if (l.isEmpty) | None | else | Some(l.init, l.last) | } | }defined module $colon$plus
scala> List(1, 2, 3) match { | case List(7) => "never" | case init :+ last => "good" | case head :: tail => "bad" | }warning: there were 1 unchecked warnings; re-run with -unchecked for detailsres0: String = good