Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of path-dependent type as a class parameter

Tags:

scala

I want to have a class which takes a parameter with a class-dependent type, as I've often done for methods. However, to my surprise, this didn't work:

scala> trait Compiler { trait Config }
defined trait Compiler

// works fine, as expected
scala> def f(c: Compiler)(conf: c.Config) = {}
f: (c: Compiler)(conf: c.Config)Unit

scala> class F(val c: Compiler)(val conf: c.Config)
<console>:8: error: not found: value c
       class F(val c: Compiler)(val conf: c.Config)
                                          ^

Why? And are there any workarounds?

like image 500
Alexey Romanov Avatar asked Mar 14 '23 16:03

Alexey Romanov


1 Answers

A workaround which seems acceptable (can't create an invalid F without additional casts):

class F private (val c: Compiler)(_conf: Compiler#Config) {
  def conf = _conf.asInstanceOf[c.Config]
}

object F {
  def apply(c: Compiler)(conf: c.Config) = new F(c)(conf)
}
like image 168
Alexey Romanov Avatar answered Mar 18 '23 03:03

Alexey Romanov