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 of 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 actually is T in the object and what am I allowed to pass through ?
As you say,
TinAis abstract; therefore you won’t find any value you can put into methodf, until you have a subtype ofAwhich actually fixesT.The idea is that the type can be successively refined:
But of course you’re question is good — there is no reason why you would want a concrete instantiation of a class whose type parameter is not fixed. I can’t think of a case where that makes sense. (Well of course, you could still have functionality that is accessible, if it does not involve type
T)Further searching finds the following, quasi duplicate, SO question. You can find there a reference to a Scala ticket which outlines it as a ‘feature’ — still doesn’t show a case where this ‘feature’ is actually useful 🙂