Type-independent type value not found

Maybe I need to update the dependent types, but I do not 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 : Now I see how to get around this. However, I would like to know why the above does not work?

+4
source share
4 answers

This is recorded as question 5712 , which is for some reason called an "improvement" rather than an error. Therefore, at present, constructors cannot resolve dependent types with multiple argument lists.

+1
source

Another workaround:

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

handler, , code. , , ?

trait Code[In, Out]

trait Handler[In, Out]

class Foo[In, Out](val code: Code[In, Out])(handler: Option[Handler[In, Out]])
+1

Maybe these simpler approaches would 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]])
  }
}
+1
source

Source: https://habr.com/ru/post/1544493/


All Articles