Case class 构造函数参数类型取决于前一个参数值
Case class constructor argument type depending on the previous argument value
我正在尝试执行以下操作
trait Stateful {
type State
}
case class SystemState(system: Stateful, state: system.State) // does not compile
即state
的类型取决于system
的(值)。但是,不支持:
illegal dependent method type: parameter appears in the type of another parameter in the same section or an earlier one
使用函数参数,我可以将参数分成两个参数列表,这对于 case class 构造函数是不可能的:
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)
我正在尝试执行以下操作
trait Stateful {
type State
}
case class SystemState(system: Stateful, state: system.State) // does not compile
即state
的类型取决于system
的(值)。但是,不支持:
illegal dependent method type: parameter appears in the type of another parameter in the same section or an earlier one
使用函数参数,我可以将参数分成两个参数列表,这对于 case class 构造函数是不可能的:
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)