Pattern matching and implicit conversion

345 views
Skip to first unread message

Alex Boisvert

unread,
Jan 28, 2013, 2:45:06 PM1/28/13
to scala-user
Scala doesn't seem to consider implicit conversions in a pattern match when looking for unapply/unapplySeq methods, e.g.,

scala> class Foo
defined class Foo

scala> implicit class Bar(f: Foo) {
     |   def unapply(x: Int) = Some(x)
     | }
defined class Bar

scala> val F = new Foo
F: Foo = Foo@437e6107

scala> 42 match { case F(x) => x }
<console>:11: error: value F is not a case class constructor, nor does it have an unapply/unapplySeq method
              42 match { case F(x) => x }
                              ^
I'm wondering if it's an explicit design choice -- and if so, what is the reasoning behind this choice -- or maybe it's just been that way and not been deemed a desirable feature.

Any thoughts?

Jason Zaugg

unread,
Jan 28, 2013, 4:11:01 PM1/28/13
to Alex Boisvert, scala-user
It's certainly desirable; I implemented it once, but I can't locate the branch anymore. But as with all implicits, you lose some measure of type safety in the process.

In particular, it would be nice to convert (X => Option[Y]) to an extractor.

-jason

Simon Ochsenreither

unread,
Jan 28, 2013, 5:56:59 PM1/28/13
to scala...@googlegroups.com
Hi Alex,

this might be useful: https://issues.scala-lang.org/browse/SI-4175

Bye,

Simon
Reply all
Reply to author
Forward
0 new messages