I’m trying to inject some Scala code into my existing Java app. (So, being said, I want some more fun).
I create a singleton stuff in Scala
ScalaPower.scala
package org.fun
class ScalaPower
object ScalaPower{
def showMyPower(time:Int) = {
(0 to time-1).mkString(", ")
}
}
Now, inside OldJava.java
class OldJava {
public void demo(){
System.out.println(?)
}
}
What should I fill in ? so that Java will call the showMyPower method?
I tried both org.fun.ScalaPower.showMyPower(10) and org.fun.ScalaPower.getInstance().showMyPower(10) but none work.
(Decompile the class file using Jad show me nothing but nonsense code.)
Edit
I remove the class ScalaPower declaration and scala produce the static method as expected. (call to org.fun.ScalaPower.showMyPower(10) just works).
Wonder if it’s a bug in scala compiler or not
I think this indirectly covers it:
As found here: http://programming-scala.labs.oreilly.com/ch06.html
In short because your Object is a companion object (has a companion class) you can’t call it like you expect. As you found if you get rid of the class it will work.