I have a trait and some classes extending it. The problem is that all method in the trait should be implemented statically, so I put them in the corresponding companion objects. I wrote something like:
A.scala
trait A
C1.scala
class C1 extends A {
override def F = C1.F
}
object C1 extends A {
def F: String = ???
}
...
As the method F has to be implemented twice, I added a corresponding companion trait.
Now I have something like:
A.scala
trait A
AC.scala
trait AC {
def F: String
}
C1.scala
class C1 extends A {
}
object C1 extends AC {
def F: String = ???
}
C2.scala
class C2 extends A {
}
object C2 extends AC {
def F: String = ???
}
...
I feel a little strange because there is nothing but an empty trait A in A.scala. I wonder if there is any better way to organize the codes.
Ahowever there is no way to force something to have a companion object and thus no way to force it to extend some trait. - However, IMHO, everytime you need something like this, you can use a Typeclass instead.class C1 extends Aandobject C1 extends AI will have to write the implementation of methodFtwice.Amark anything? If not, you can simply delete it without any loss.Adefined the set of available input of a not-mentioned function. I am writing the instances ofAlikeC1, but I don't want to extends it twice.