Question

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!

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top