Type aliasing Java classes with statics

Suppose MyClass is a class defined in Java and has many static and non-static members. I tried using this class (and associated companion object) in a Scala MyObject , as shown below:

 object MyObject { import javastuff._ type MyAlias = MyClass val MyAlias = MyClass } 

Scalak complains:

 error: object MyClass is not a value val MyAlias = MyClass 

How do I get around this? Thanks.

+7
source share
2 answers

Although this works in pure Scala for a class + companion object, this is not possible using static Java methods, since they do not belong to any interface.

Scala could theoretically create an object containing delegates for all static methods of a certain class, but currently it does not. It is also possible to write a compiler plugin for this if you are more comfortable writing plugins.

Otherwise, you will either have to create an object full of delegates, or simply cherry-pick several methods and pass them as functions.

+6
source

this is not possible with static Java methods, since they do not belong to any interface.

Update after 5 years: PR 5131 mentions:

We used to disable the generation of static forwarders when the object had a companion, since it was impossible to add methods with bodies to the interface in JVM 6.

The JVM removed this restriction in order to support default methods in interfaces, so we can also remove the restriction on static forwarders.

Corrections scala -dev issue 59

See commit 41c9a17 by Jason Zaug ( retronym ) .

0
source

All Articles