when defining a type hierarchy of case classes in scala:
sealed trait FooBar {
def A:Int
def B:Int
def C:Int
}
// works
final case class Bar(A:Int, B:Int, C:Int)extends FooBar
// fails
final case class Bar extends FooBar(A:Int, B:Int, C:Int)
// fails
final case class Foo extends FooBar
how can I avoid to specify the already inherited parameters when defining an inherited type? Is this possible without any macros: Scala case classes and constructors
Would an abstract class be better suited for this purpose?
Well, what you've declared is a trait with three abstract methods.
Your first implementation declares a case class with 3 values that match the abstract methods declared in FooBar
. Since methods without parenthesis and values are basically the same in Scala, this works.
Your second implementation calls FooBar
's constructor with 3 values that don't exist. Where does it get the A
from?
Your third implementation is declaring a concrete class that does not implement abstract methods, and cannot compile.
I do not know of a (sane) solution for what you're asking. You're declaring abstract methods and want to not implement them. It's probably feasible through macros, but it seems like a lot of work for not much benefit.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.