質問

I was wondering if there is a nicer way of doing this:

sealed abstract class Edge(val id: String)
case class LabeledEdge(override val id: String, label: String)
    extends Edge(id)
case class WeightedEdge(override val id: String, weight: Double)
    extends Edge(id)

My issue is that I have quite a lot of members other than id in the class represented here as Edge. I have to write override val before each member defined in the abstract class which makes it quite cumbersome, and thought there should be some nicer way of doing this.

Thanks in advance!

役に立ちましたか?

解決

You could use trait with abstract defs (or vals) and no constructor:

sealed trait Edge{ def id(): String } // or { val id: String }
case class LabeledEdge(id: String, label: String) extends Edge
case class WeightedEdge(id: String, weight: Double) extends Edge
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top