How to invoke a concrete Scala trait method from Java?

坚强是说给别人听的谎言 提交于 2019-12-24 00:53:37

问题


I have a Java / Scala mix Maven project. I need to reuse a Saddle method make that is concretely defined as part of a trait called Index. The method is defined here if that helps in any way. I have tried calling that method from java using Index.make or Index$class.make but in both cases I get the error: cannot find symbol compilation error.

Is there a way to call a concrete Trait method from Java?


回答1:


A trait is similar to a Java interface in a sense that it is not a concrete constructor.

From Scala:

class IndexImpl extends Index[SomeParamtersHere] {}
val x = new ClassImpl
x.make(..)

Or use an object:

object Index extends Index {}
Index.make(..)

From Java:

Traits with concrete implementations will compile to the appropriate abstract class. If certain members don't have concrete implementation, you have to implement them.

import org.saddle.Index
public class TraitImpl extends Index$class {
}



回答2:


OK I found the answer. Calling it like this works but only because the Index trait already has an Index object available (that I didn't notice before):

Index$.MODULE$.make(rrule, startDate, endDate);


来源:https://stackoverflow.com/questions/22356442/how-to-invoke-a-concrete-scala-trait-method-from-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!