문제

Given simple class hierarchy

abstract sealed class Base(id: String)

case class Child1(id: String, value: Int) extends Base(id)

case class Child2(id: String, value: Long, file: File) extends Base(id)

can I use macros or something like that to avoid passing id to Base (and instruct compiler to generate this for me)? With single argument it's not that hard to pass it, but in case of several arguments it becomes uncomfortable.

Or if I could omit specification of id in child classes and make compiler generating ones for me from base class?

도움이 되었습니까?

해결책

You can make Base as a trait:

sealed trait Base {
  val id: String
}

case class Child1(id: String, value: Int) extends Base
case class Child2(id: String, value: Long, file: File) extends Base
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top