I'm a beginner programmer working within a mixed Java/Scala codebase. I have a implicit class in Scala that provides extensions to a class within a third-party dependency. The implicit class is as so:
object ThirdPartyClassOps {
implicit class ThirdPartyClassOps(val tp: ThirdPartyClass) extends AnyVal {
...
def generateKey(): String = {
tp.getId + "-" + tp.getOtherString
}
}
}
Where generateKey
is the method I am interested in. My Java class where I'd like to call generateKey
is as so:
public class MyJavaClass {
...
public static boolean thirdPartyClassIsValid(ThirdPartyClass tp) {
...
// Generate tp key here.
}
}
My beginner's understanding of JVM languages indicates that it should be possible for me to call generateKey
from my Java class. Searching for answers has led me to examples such as here where calling said Scala method has a simple solution. When I attempt to call it however using String myKey = ThirdPartyClassOps.ThirdPartyClassOps(tp).generateKey();
or String myKey = ThirdPartyClassOps$.MODULE$.ThirdPartyClassOps(tp).generateKey();
as suggested by the StackOverflow answer and IntelliJ's auto-suggest, I run into "error: cannot find symbol" error when attempting to build:
.../MyJavaClass.java:33: error: cannot find symbol
String myKey = ThirdPartyClassOps$.MODULE$.ThirdPartyClassOps(tp).generateKey();
^
symbol: method generateKey()
location: class ThirdPartyClass
Given the implicit class definition in Scala, how can I call my implicit class method within my Java class?