Why does scala.util.matching.Regex#unapplySeq take Any?

519 views
Skip to first unread message

OlegYch

unread,
Sep 17, 2012, 11:03:32 PM9/17/12
to scala...@googlegroups.com

Imo this snippet 

 val r = "(\\d+)".r; List(1) collect { case r(i) => i }

should not compile.

Should I submit a bug, is there any chance this is going to be fixed?

Thanks, Oleg.

Som Snytt

unread,
Sep 18, 2012, 11:32:54 AM9/18/12
to OlegYch, scala...@googlegroups.com
Is it sufficient to split the unapply into unapplySeq(CharSequence) and unapplySeq(Match)?

Is it desirable?

scala> val r = "(\\d+)".r; List(1) collect { case r(i) => i }
<console>:9: error: cannot resolve overloaded unapply

        List(1) collect { case r(i) => i }
                               ^
<console>:9: error: not found: value i

        List(1) collect { case r(i) => i }
                                       ^

scala> val t = "Last modified 2011-07-15"
t: String = Last modified 2011-07-15

scala> val dateP1 = """(\d\d\d\d)-(\d\d)-(\d\d)""".r
dateP1: scala.util.matching.Regex = (\d\d\d\d)-(\d\d)-(\d\d)

scala> val copyright: Option[String] = for {
     | dateP1(year, month, day) <- dateP1 findFirstIn t
     | } yield year
copyright: Option[String] = Some(2011)

scala> val copyright: Option[String] = for {
     | dateP1(year, month, day) <- dateP1 findFirstMatchIn t
     | } yield year
copyright: Option[String] = Some(2011)

scala> val ns = List("1,2","x","3,4")
ns: List[String] = List(1,2, x, 3,4)

scala> val r = "(\\d+)".r
r: scala.util.matching.Regex = (\d+)

scala> (ns map (r findFirstMatchIn _)).flatten
res0: List[scala.util.matching.Regex.Match] = List(1, 3)

scala> res0 collect { case r(i) => i }
res1: List[String] = List(1, 3)

scala> ns map (s => (r findFirstIn s) getOrElse s)
res2: List[String] = List(1, x, 3)

scala> ns map (s => (r findFirstMatchIn s) getOrElse s)
res3: List[Object] = List(1, x, 3)

scala> res3 collect { case r(i) => i }
<console>:11: error: cannot resolve overloaded unapply
              res3 collect { case r(i) => i }
                                  ^
<console>:11: error: not found: value i
              res3 collect { case r(i) => i }
                                          ^

Oleg Aleshko

unread,
Sep 18, 2012, 3:17:38 PM9/18/12
to Som Snytt, scala...@googlegroups.com
Hi Som.

I also think splitting into multiple overloads is the way to go, although
i'm not sure why have it for Match altogether.
Just wonder what might be the consequences and what was the original
reasoning.

Oleg.

Daniel Sobral

unread,
Sep 18, 2012, 7:39:03 PM9/18/12
to Oleg Aleshko, Som Snytt, scala...@googlegroups.com
On Tue, Sep 18, 2012 at 4:17 PM, Oleg Aleshko <oleg...@gmail.com> wrote:
> Hi Som.
>
> I also think splitting into multiple overloads is the way to go, although
> i'm not sure why have it for Match altogether.
> Just wonder what might be the consequences and what was the original
> reasoning.

I presume you don't use the Match methods? Why *wouldn't* a regex be
able to unapply a regex match?

I see a very good reason to have it split: not matching the same thing
twice. Unfortunately, that opportunity is presently squandered.
--
Daniel C. Sobral

I travel to the future all the time.

Daniel Sobral

unread,
Sep 18, 2012, 7:44:01 PM9/18/12
to Oleg Aleshko, Som Snytt, scala...@googlegroups.com
I take that back. The regex that produced the Match and the one doing
the unapplySeq might be different.

Som Snytt

unread,
Sep 18, 2012, 9:13:54 PM9/18/12
to Daniel Sobral, Oleg Aleshko, scala...@googlegroups.com

I had the same two thoughts about reusing the Match, and in the same order, but I took longer to reach the conclusion.

I have no experience with the API; I've only looked at it twice when someone asks an interesting question.  Thank goodness for the great Scaladoc.

However, I'll try the following and report back.

  def unapplySeq(m: Match): Option[List[String]] =
    if (m.matched != null) {
      if (m.matcher.pattern == this.pattern) Some((1 to m.groupCount).toList map m.group)
      else unapplySeq(m.matched)
    } else None

Som Snytt

unread,
Sep 19, 2012, 5:42:27 AM9/19/12
to Daniel Sobral, Oleg Aleshko, scala...@googlegroups.com
Is this change considered useful?  If so, I'll follow up.

https://github.com/som-snytt/scala/tree/topic/regex-unapply

OlegYch

unread,
Sep 19, 2012, 5:13:32 PM9/19/12
to scala...@googlegroups.com, Daniel Sobral, Oleg Aleshko
Thanks, Som!
This looks good to me.

Oleg.

OlegYch

unread,
Sep 26, 2012, 8:32:15 PM9/26/12
to scala...@googlegroups.com, Daniel Sobral, Oleg Aleshko
Hi Som!
Somehow i thought you were going to submit a pr.
Have you encountered any additional complications? Should we bring those to scala-internals?

I really hope that would make its way into 2.10.

Oleg.

Som Snytt

unread,
Sep 26, 2012, 11:27:55 PM9/26/12
to OlegYch, scala...@googlegroups.com, Daniel Sobral
On Wed, Sep 26, 2012 at 5:32 PM, OlegYch <oleg...@gmail.com> wrote:
Hi Som!
Somehow i thought you were going to submit a pr.
Have you encountered any additional complications? Should we bring those to scala-internals?

I really hope that would make its way into 2.10.



It will take a while before it is vetted.  Maybe you meant 2.10.x.

But feel free to press your case.

Reply all
Reply to author
Forward
0 new messages