Question

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
Was it helpful?

Solution

Singleton types can only refer to AnyRef descendants. See Why do String literals conform to Scala Singleton for more details.

The argument the application id(BigInt(9)) can't be referred to via a stable path, so consequently doesn't have an interesting singleton type.

scala> id(new {})
res4: AnyRef = $anon$1@7440d1b0

scala> var o = new {}; id(o)
o: Object = $anon$1@68207d99
res5: AnyRef = $anon$1@68207d99

scala> def o = new {}; id(o)
o: Object
res6: AnyRef = $anon$1@2d76343e

scala> val o = new {}; id(o) // Third time's the charm!
o: Object = $anon$1@3806846c
res7: o.type = $anon$1@3806846c

OTHER TIPS

I get error on #2 too (using Scala 2.9.1.final):

error: illegal dependent method type
  def id(x: AnyRef): x.type = x;          ^
one error found

I believe the correct solution is to use make id polymorphic using a type parameter:

def id[T <: Any](x: T): T = x;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top