How to invoke a concrete Scala trait method from J

2019-07-10 02:07发布

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?

2条回答
老娘就宠你
2楼-- · 2019-07-10 02:48

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 {
}
查看更多
来,给爷笑一个
3楼-- · 2019-07-10 02:50

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);
查看更多
登录 后发表回答