Ostensible inconsistencies in singleton types

Viewed 223

I have a couple of questions about singleton types, but since they're both very closely related, I am posting them under the same thread.

Q1. Why does #1 does not compile but #2 does?

def id(x: Any): x.type = x      // #1
def id(x: AnyRef): x.type = x   // #2

Q2. The type is correctly inferred in case of String but not in case of other reference types I tried. Why is that so?

scala> id("hello")
res3: String = hello

scala> id(BigInt(9))
res4: AnyRef = 9

scala> class Foo
defined class Foo

scala> id(new Foo)
res5: AnyRef = Foo@7c5c5601
2 Answers
Related