- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Strange type matching
Fri, 2009-09-11, 10:46
Hi,
I just came across the following type match problem:
object MatchTest {
def dispatch(any: Any) =
any match {
case anyRef: AnyRef => "AnyRef"
case anyVal: AnyVal => "AnyVal"
}
def main(args : Array[String]) : Unit = {
println("Boolean: " + dispatch(true))
println("Integer: " + dispatch(1))
println("List: " + dispatch(List()))
}
}
with the output
Boolean: AnyRef
Integer: AnyRef
List: AnyRef
Am I correct in assuming that this is compiler bug, as I would expect a
Boolean, as well as an Integer, to be a subtype of AnyVal?
Cheers, Olaf Burkart
Fri, 2009-09-11, 14:57
#2
Re: Strange type matching
Not type erasure, actually. Type erasure is something that happens to parameterized types. This is a consequence of JVM not handling Java primitive types as classes, which clashes with Scala's conception of everything being an object.
On Fri, Sep 11, 2009 at 7:16 AM, Kevin Wright <kev [dot] lee [dot] wright [at] googlemail [dot] com> wrote:
--
Daniel C. Sobral
Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
On Fri, Sep 11, 2009 at 7:16 AM, Kevin Wright <kev [dot] lee [dot] wright [at] googlemail [dot] com> wrote:
This is type erasure again... In the JVM, AnyVal == AnyRef == Object
On Fri, Sep 11, 2009 at 10:46 AM, Olaf Burkart <olaf [dot] burkart [at] itemis [dot] de> wrote:
Hi,
I just came across the following type match problem:
object MatchTest {
def dispatch(any: Any) =
any match {
case anyRef: AnyRef => "AnyRef"
case anyVal: AnyVal => "AnyVal"
}
def main(args : Array[String]) : Unit = {
println("Boolean: " + dispatch(true))
println("Integer: " + dispatch(1))
println("List: " + dispatch(List()))
}
}
with the output
Boolean: AnyRef
Integer: AnyRef
List: AnyRef
Am I correct in assuming that this is compiler bug, as I would expect a Boolean, as well as an Integer, to be a subtype of AnyVal?
Cheers, Olaf Burkart
--
Daniel C. Sobral
Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
Fri, 2009-09-11, 15:27
#3
Re: Strange type matching
Guess I never thought of it that way...
I just have this little mental box labeled "lack of reification" which I use to hold this sort of problem, along with a suitable choice of expletives :)
On Fri, Sep 11, 2009 at 2:46 PM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:
I just have this little mental box labeled "lack of reification" which I use to hold this sort of problem, along with a suitable choice of expletives :)
On Fri, Sep 11, 2009 at 2:46 PM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:
Not type erasure, actually. Type erasure is something that happens to parameterized types. This is a consequence of JVM not handling Java primitive types as classes, which clashes with Scala's conception of everything being an object.
On Fri, Sep 11, 2009 at 7:16 AM, Kevin Wright <kev [dot] lee [dot] wright [at] googlemail [dot] com> wrote:
This is type erasure again... In the JVM, AnyVal == AnyRef == Object
On Fri, Sep 11, 2009 at 10:46 AM, Olaf Burkart <olaf [dot] burkart [at] itemis [dot] de> wrote:
Hi,
I just came across the following type match problem:
object MatchTest {
def dispatch(any: Any) =
any match {
case anyRef: AnyRef => "AnyRef"
case anyVal: AnyVal => "AnyVal"
}
def main(args : Array[String]) : Unit = {
println("Boolean: " + dispatch(true))
println("Integer: " + dispatch(1))
println("List: " + dispatch(List()))
}
}
with the output
Boolean: AnyRef
Integer: AnyRef
List: AnyRef
Am I correct in assuming that this is compiler bug, as I would expect a Boolean, as well as an Integer, to be a subtype of AnyVal?
Cheers, Olaf Burkart
--
Daniel C. Sobral
Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
Sat, 2009-09-12, 18:27
#4
Re: Strange type matching
Yeah, scala trying to rectify primitives with objects is very handy inside scala, but can cause strange issues when interfacing Java + Scala.
e.g. we use Scala 2.7.4 in production at work. I find myself sometimes having to explicitly declare overridden methods with java.lang.Long instead of just Long. I also find that when using java collections, you always have to explicitly state java's "big" Long instead of scala's Long. It's a minor annoyance, as sometimes this also kills the Rich* implicits.
That's why:
Pro Scala Tip #XYZ- When mixing Scala and Java, prefer primitives in your Java interfaces.
- Josh
On Fri, Sep 11, 2009 at 10:23 AM, Kevin Wright <kev [dot] lee [dot] wright [at] googlemail [dot] com> wrote:
e.g. we use Scala 2.7.4 in production at work. I find myself sometimes having to explicitly declare overridden methods with java.lang.Long instead of just Long. I also find that when using java collections, you always have to explicitly state java's "big" Long instead of scala's Long. It's a minor annoyance, as sometimes this also kills the Rich* implicits.
That's why:
Pro Scala Tip #XYZ- When mixing Scala and Java, prefer primitives in your Java interfaces.
- Josh
On Fri, Sep 11, 2009 at 10:23 AM, Kevin Wright <kev [dot] lee [dot] wright [at] googlemail [dot] com> wrote:
Guess I never thought of it that way...
I just have this little mental box labeled "lack of reification" which I use to hold this sort of problem, along with a suitable choice of expletives :)
On Fri, Sep 11, 2009 at 2:46 PM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:Not type erasure, actually. Type erasure is something that happens to parameterized types. This is a consequence of JVM not handling Java primitive types as classes, which clashes with Scala's conception of everything being an object.
On Fri, Sep 11, 2009 at 7:16 AM, Kevin Wright <kev [dot] lee [dot] wright [at] googlemail [dot] com> wrote:
This is type erasure again... In the JVM, AnyVal == AnyRef == Object
On Fri, Sep 11, 2009 at 10:46 AM, Olaf Burkart <olaf [dot] burkart [at] itemis [dot] de> wrote:
Hi,
I just came across the following type match problem:
object MatchTest {
def dispatch(any: Any) =
any match {
case anyRef: AnyRef => "AnyRef"
case anyVal: AnyVal => "AnyVal"
}
def main(args : Array[String]) : Unit = {
println("Boolean: " + dispatch(true))
println("Integer: " + dispatch(1))
println("List: " + dispatch(List()))
}
}
with the output
Boolean: AnyRef
Integer: AnyRef
List: AnyRef
Am I correct in assuming that this is compiler bug, as I would expect a Boolean, as well as an Integer, to be a subtype of AnyVal?
Cheers, Olaf Burkart
--
Daniel C. Sobral
Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.










On Fri, Sep 11, 2009 at 10:46 AM, Olaf Burkart <olaf [dot] burkart [at] itemis [dot] de> wrote: