Scala – Abstract static methods in Scala

abstract classscalastatic methods

I've read this relevant post, but there weren't many concrete answers (poor language design more or less):
Why can't static methods be abstract in Java

I'm a bit of a newcomer to Scala, is this possible in it (maybe with traits or something)?

I tried having my base class extend a trait, but then child classes are required to implement the abstract static method as a member method, when I really want them to be required to be implemented in the companion object.

Best Answer

There aren't static methods in Scala [*], so your question is moot.

However, you can get what you want by extending an object with a trait:

scala> trait A { def foo(): Int }
defined trait A

scala> object C extends A { def foo(): Int = 5 }
defined module C

scala> C.foo
res0: Int = 5

which probably does what you want. There isn't really any way to force something to be implemented in the companion object of a class. The companion object might not exist.

[*] Technically, there are, but this is more of an implementation detail than an overall philosophy. See Method in companion object compiled into static methods in scala?