This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Bug in pattern matcher with list and custom extractor?

2 replies
huynhjl 2
Joined: 2011-03-26,
User offline. Last seen 42 years 45 weeks ago.

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?

moors
Joined: 2010-10-06,
User offline. Last seen 36 weeks 4 days ago.
Re: Bug in pattern matcher with list and custom extractor?
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 -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
huynhjl 2
Joined: 2011-03-26,
User offline. Last seen 42 years 45 weeks ago.
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

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland