Abstract static methods in Scala

I read this important post, but there were few answers (design of poor languages ​​more or less): Why static methods cannot be abstract in Java

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

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

+6
source share
3 answers

Scala [*] has no static methods, so your question is moot.

However, you can get what you want by expanding the object with the attribute:

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 is really no way to force something to be implemented in a companion class object. A companion object may not exist.

[*] Technically, there is, but these are more likely implementation details than a general philosophy. See Method in companion compiled into static methods in scala?

+4
source

There are two possible explanations for why abstract static methods are not possible in Scala, Java, C ++, or C #.

First technical: abstract / virtual methods require an object reference (called this) to select the override to be performed. You do not provide such an object when invoking a static method.

The second is logical: abstract / virtual static methods make no sense. When you call a static method, you always know the type that this method contains. You write:

 MyClass.DoSomething(args) 

If you have a MyDerivative that extends MyClass, you can simply define another static method:

 MyDerivative.DoSomethingDifferent(args) 

Static virtual methods simply do not make sense, since they will work like regular static methods.

0
source

I'm not sure what you would like to do with an abstract static method in java, but the only potential use case that I saw in the past (I would like me to remember by whom ...) calls the method directly by a parameter of a general type.

i.e. if java allowed something like this ...

 // this is not valid java... // let pretend we can define a static method in an interface interface Foo { static String Foo(); } // a class that implements our interface class FooImpl implements Foo { public static String Foo() {return "foo";} } 

... we could use it for a generic parameter to call Foo () directly by type

 static <T extends Foo> String Test() { return T.Foo(); // even if our hypothetical static interface // was a valid thing, this would probably still fail // due to type erasure } 

This would make a little more sense in C #, because:

This basically means that in “pretend C #” with static interfaces you can use something that matches the above “pretend java” to write a generic method that works on any type that defines a particular operator (for example, everything what the Operator "+" has).

Now back to scala. How does scala address this script? This is partially not the case. scala has no static methods: the object is singleton (i.e. a class with only one instance), but still it is a class with the usual instance methods, that is, on objects that you still call on a single instance, and not directly on type (even operators are methods in scala).

So, in scala we will write:

 trait Foo { def Foo:String } object FooImpl extends Foo { def Foo = "foo" } def Test(f: Foo) = f.Foo 

... and call our method with

 scala> Test(FooImpl) res0: String = foo // note that FooImpl is still an instance (the only one) of FooImpl and not // the type itself 

You can do some tricks with implicits to avoid passing a single instance as a parameter:

 implicit def aFoo = FooImpl def Test2(implicit f: Foo) = f.Foo 

now it works:

 scala> Test2 res1: String = foo 

With more advanced tricks with implicits, scala also defines Numeric, which allows operators to be used on any numerical value , even if they do not implement a common interface out of the box.

0
source

Source: https://habr.com/ru/post/911285/


All Articles