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

Strange type matching

4 replies
Olaf Burkart
Joined: 2009-09-11,
User offline. Last seen 42 years 45 weeks ago.

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

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 4 days ago.
Re: Strange type matching
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


dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
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:
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.
Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 4 days ago.
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:
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.

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 6 days ago.
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:
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.


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