user976845
user976845

Reputation: 31

Abstract types puzzler

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

Answers (3)

MB_
MB_

Reputation: 21

Abstract type seems to have default value Nothing.

Upvotes: 0

retronym
retronym

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

Derek Wyatt
Derek Wyatt

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

Related Questions