scala – Case类构造函数的参数类型取决于以前的参数值
发布时间:2020-12-16 09:13:48 所属栏目:安全 来源:网络整理
导读:我正在努力做以下事情 trait Stateful { type State}case class SystemState(system: Stateful,state: system.State) // does not compile 也就是说,系统的类型(的价值).但是,不支持: illegal dependent method type: parameter appears in the type of ano
我正在努力做以下事情
trait Stateful { type State } case class SystemState(system: Stateful,state: system.State) // does not compile 也就是说,系统的类型(的价值).但是,不支持:
使用函数参数,我可以将参数分为两个参数列表,这对于case类构造函数是不可能的: def f(system: Stateful)(state: system.State): Unit = {} // compiles 我能做的最好的是: case class SystemState[S](system: Stateful { type State = S },state: S) // compiles 但是我认为没有一个类型参数应该是可能的,因为在dotty中,我想类型参数被禁止键入成员. 我的问题是,这可以表达没有类型参数吗? 在更一般的上下文中,我正在探索类型参数可以在什么程度上被类型成员替代,什么时候这样做是个好主意. 解决方法
对于依赖类型的多参数列表方法不幸是
not supported for constructors,所以不,你将不得不引入一个类型参数.
你可以隐藏这个事实,如果它打扰了,但是 trait Stateful { type State } object SystemState { def apply(system: Stateful)(state: system.State): SystemState = new Impl[system.State](system,state) private case class Impl[S](val system: Stateful { type State = S },val state: S) extends SystemState { override def productPrefix = "SystemState" } } trait SystemState { val system: Stateful val state: system.State } case object Test extends Stateful { type State = Int } val x = SystemState(Test)(1234) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |