Compiler error when using abstract types
- by Dylan
I'm trying to implement a "protocol helper" trait that is responsible for matching up Prompts and Responses. The eventual goal is to have an object that defines the various Prompt and Response classes as subclasses of a sealed trait, then have a class that mixes in the ProtocolSupport trait for that Protocol object. The problem is that my current approach won't compile, even though I'm fairly sure it should.
Here's a distilled version of what I've got:
trait Protocol {
type Response
type Prompt <: BasePrompt
trait BasePrompt {
type Data
def validate(response: Response): Validated[Data]
}
}
trait ProtocolSupport[P <: Protocol] {
def foo(prompt: P#Prompt, response: P#Response) = {
// compiler error
prompt.validate(response)
}
}
The compiler doesn't like the response as an argument to prompt.validate:
[error] found : response.type (with underlying type P#Response)
[error] required: _4.Response where val _4: P
[error] prompt.validate(response)
[error] ^
This isn't very helpful.. it seems to say that it wants a P.Response but that's exactly what I'm giving it, so what's the problem?