Jay Taylor's notes
back to listing indexAbstract static methods in Scala - Stack Overflow
[web search]
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. | |||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||
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:
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? | |||||||||||||||||||||||||||||||||
|
There are two possible explanations of why abstract static methods are not possible in Scala, Java, C++ or C#. First is technical: abstract/virtual methods require a reference to an object (called this) to choose an override that will run. You provide no such object when you call a static method. Second is logical: abstract/virtual static methods don't make any sense. When you call a static method, you always know the type which contains that method. You write:
If you have MyDerivative that extends MyClass, you can just define another static method:
There is just no point in static virtual methods, since they will work just as ordinary static methods. | |||||||||
|
I'm not sure what you would like to do with an abstract static method in java, but the only potential use case I've seen described in the past (I wish I remembered by whom...) is calling a method directly on a generic type parameter. i.e. if java allowed something like this...
...we could use it on a generic parameter, to call Foo() directly on the type
This would make somewhat more sense in C# because:
Basically this means in a "pretend C#" with static interfaces, you could use something corresponding to the above "pretend java" code to write a generic method that works on any type that defines a specific operator (e.g. everything that has a "+" operator). Now, back to scala. How does scala address this scenario? In part it doesn't. Scala has no static methods: an object is a singleton (i.e. a class with only one instance) but still a class with normal, instance methods, i.e. on objects you're still calling methods on the only instance, not directly on the type (even operators are methods in scala). So in scala we would write:
...and call our method with
You can do some tricks with implicits to avoid passing the only instance as a parameter:
now this works:
With more advanced tricks with implicits, scala also defines Numeric, that allows you to use operators on any numeric value, even though they don't implements a common interface out of the box.
| ||||
add comment |