Using an abstract type in a concrete class?
scala> class A { type T <: String; def f(a: T) = println("foo")} defined class A scala> (new A).f("bar") <console>:9: error: type mismatch; found : java.lang.String("bar") required: _1.T where val _1: A (new A).f("bar") ^ Class A has an abstract type T , but is not an abstract class. Creating an object A (as shown) does not define type T
My first thought was: I am allowed to pass any type as T , which is a subclass of String, but I am not. So what type is T actually in the object and what can I go through?
As you say, T in A is abstract; therefore, you will not find any value that you can put in the method f until you have a subtype A that actually corrects T
(new A { type T = String }).f("bar") The idea is that the type can be sequentially refined:
trait Var[A] { def get: A; def set(v: A): Unit } trait Sys { type V[A] <: Var[A] def swap[A](x: V[A], y: V[A]): Unit = { val tmp = x.get x.set(y.get) y.set(tmp) } } trait HistVar[A] extends Var[A] { def created: java.util.Date } trait HistSys extends Sys { type V[A] <: HistVar[A] def newest[A](x: V[A], y: V[A]): A = if (x.created after y.created) x.get else y.get } But, of course, you doubt it - there is no reason why you will need a specific instance of a class whose type parameter is not fixed. I can not think of a case where it makes sense. (Well, of course, you can still have functionality available if it doesn't include type T )
Further search finds the following, quasi-public, SO question . You can find a link to the Scala ticket that describes this as a “function” - still does not show the case when this “function” is really useful :)