Reputation: 67280
Maybe I need a refresher on dependent types, but I don't understand why the following does not work:
trait Code { type In; type Out }
trait Handler[In, Out]
class Foo(val code: Code)(handler: Option[Handler[code.In, code.Out]])
Error:
<console>:52: error: not found: value code
class Foo(val code: Code)(handler: Option[Handler[code.In, code.Out]])
^
<console>:52: error: not found: value code
class Foo(val code: Code)(handler: Option[Handler[code.In, code.Out]])
^
Edit: I can see how to work around this, now. Still I would like to know why the above does not work?
Upvotes: 4
Views: 223
Reputation: 67280
This is a logged as issue 5712 which for some reason is called an "improvement" not bug. So currently constructors cannot resolve dependent types with multiple argument lists, it seems.
Upvotes: 1
Reputation: 432
May be one these more simple approaches will be suitable:
object Main {
def main(args: Array[String]): Unit = {
demo1
demo2
}
def demo1 {
trait Code { type In; type Out }
trait Handler[In, Out]
class Foo(val code: Code) {
private val handler: Option[Handler[code.In, code.Out]] = ???
}
}
def demo2 {
trait Code[In, Out]
trait Handler[In, Out]
class Foo[In, Out](val code: Code[In, Out])(handler: Option[Handler[In, Out]])
}
}
Upvotes: 1
Reputation: 39356
Another workaround:
trait Foo {
val code: Code
val handler: Handler[code.In, code.Out]
}
Upvotes: 2
Reputation: 55569
The handler
parameter seems to have no knowledge of the code
parameter. Would you be able to accomplish the same thing defining your traits like this?
trait Code[In, Out]
trait Handler[In, Out]
class Foo[In, Out](val code: Code[In, Out])(handler: Option[Handler[In, Out]])
Upvotes: 1