0__
0__

Reputation: 67280

Path-dependent type's value not found

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

Answers (4)

0__
0__

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

user4298319
user4298319

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

Owen
Owen

Reputation: 39356

Another workaround:

trait Foo {
  val code: Code
  val handler: Handler[code.In, code.Out] 
}

Upvotes: 2

Michael Zajac
Michael Zajac

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

Related Questions