Reputation: 31
Given class A
is:
class A {
type R
}
Why does the following code compile (and run too)?
val a = new A
println(a)
Isn't A
supposed to be abstract?
Upvotes: 3
Views: 154
Reputation: 55028
Just to elaborate on @alex22's comment:
scala> trait T { type R; def foo(r: R) = r }
defined trait T
scala> new T{}.foo("")
<console>:12: error: type mismatch;
found : java.lang.String("")
required: _6.R where val _6: java.lang.Object with T
new T{}.foo("")
^
scala> new T{ type R = String }.foo("")
res37: java.lang.String = ""
Upvotes: 2
Reputation: 2727
A
isn't abstract. If it were abstract then it would look like:
abstract class A {
type R
}
or
trait A {
type R
}
Now, I can't find it in the spec (haven't had much luck with finding stuff in there lately) but I've seen this before. If the type isn't used, then it appears as though it's not evaluated, which means the lack of its completeness is not an issue.
If you really want A
to be abstract, make it so using one of the above definitions instead.
Upvotes: 7