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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With