Discussion:
Multiple unapply and unapplySeq in an extractor object
Doug Pardee
2008-03-06 06:00:04 UTC
Permalink
[Nobody had an answer over in scala-user, so I thought
that I'd try here. Apologies to those who already read
this over there.]

(I am using 2.6.1)

I am a bit surprised to find that unapply and
unapplySeq cannot be overloaded based on the parameter
type. Well, they can be overloaded but then the object
cannot be used as an extractor object.

I am also surprised to find that unapply and
unapplySeq cannot both exist in the same extractor
object. That seems to be at odds with section 8.1.8 of
the reference (December 19 version).

It appears to me that an extractor object must have
only one extractor method defined. This seems
unnecessarily strict.

Have I misunderstood something here?


____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
Andrew Foggin (h)
2008-03-06 07:36:15 UTC
Permalink
(IIUC) this could only work if the compiler knew the type of the thing you
are matching on and could unambiguously determine which unapply or
unapplySeq method to call. But since you can match on /anything/ this is
not possible in the general case.

--Andrew
--
View this message in context: http://www.nabble.com/-scala--Multiple-unapply-and-unapplySeq-in-an-extractor-object-tp15867089p15868015.html
Sent from the Scala mailing list archive at Nabble.com.
Doug Pardee
2008-03-07 16:47:31 UTC
Permalink
Post by Andrew Foggin (h)
(IIUC) this could only work if the compiler knew
the type of the thing you are matching on and
could unambiguously determine which unapply or
unapplySeq method to call. But since you can
match on /anything/ this is not possible in the
general case.
That answer doesn't satisfy me. In any event, the
problem with unapply overloading is fixed in
2.7.0.final. Which *does* satisfy me. :-)

I still want to know about the issue of unapply vs.
unapplySeq, which section 8.1.8 of the 2.6 and 2.7
reference manuals says should be disambiguated:
"in an extractor pattern x(p1, . . . , pn) if the
extractor object x has an unapplySeq method with a
result type conforming to Seq[S], but does not have an
unapply method that matches p1, . . . , pn."

The reference doesn't say "does not have any unapply
methods." It says "does not have an unapply method
that matches p1, . . . , pn."

Furthermore, in 2.7.0.final the presence of an unapply
method for a different argument type is enough to make
unapplySeq unusable. In other words:
def unapply(x: String) = ...
def unapplySeq(x: double) = ...
does not work when matching against a double. The
compiler complains:
error: pattern type is incompatibe with expected type;
found : double
required: String
([sic] on "incompatibe").


____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
Loading...