Case class constructor argument type depending on the previous argument value

倾然丶 夕夏残阳落幕 提交于 2019-11-30 17:43:37

The multiple parameter list approach for dependent types unfortunately is not supported for constructors, so no, you will have to introduce a type parameter.

You could hide that fact if it becomes bothering, though

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!