I would like to parameterize a type with one of its subclasses. Consider the following:
class DataLoader {
class Data { /* data specifics to this data loader */ }
def getData : Data /* and so on */
}
Now I want to make this loader able to asynchronously retrieve data from the network. One of the options is to have it subclass Callable.
class DataLoader extends Callable[Data] {
class Data { /* ... */ }
def call : Data = { ... }
}
val futureData = executor.submit(new DataLoader)
futureData.get
Scala will not let me do that, because when I supply the parameters of Callable, Data is not known yet. If I write DataLoader.Data, Scala calls me off for a cyclic reference.
Sure, I could write my data class outside of my loader, but there are cases where it's nicer inside. Sure, another alternative would be to have, say, a DataManager, with inside a Data type and a Loader which extends Callable[Data] - which in this case is arguably better design. But those issues aside, is there any way I can actually implement a trait that involves writing a function returning a T, setting T to be an inner class ?