加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 使用路径相关类型作为类参数

发布时间:2020-12-16 18:05:54 所属栏目:安全 来源:网络整理
导读:我希望有一个类,它接受一个类依赖类型的参数,就像我经常为方法做的那样.但令我惊讶的是,这不起作用: scala trait Compiler { trait Config }defined trait Compiler// works fine,as expectedscala def f(c: Compiler)(conf: c.Config) = {}f: (c: Compiler
我希望有一个类,它接受一个类依赖类型的参数,就像我经常为方法做的那样.但令我惊讶的是,这不起作用:

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)
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读