我想拥有一个带有参数的类,该参数具有与类相关的类型,就像我通常对方法所做的那样。但是,令我惊讶的是,这没有用:
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)
^
为什么?有没有解决方法?
似乎可以接受的解决方法(如果F
没有其他强制转换,则不能创建无效方法):
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)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句