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?
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With