0

How to access the CALL_INDEX object from java class:

MyStrategy.scala

trait CallStrategy 
object CallStrategy {
  case object CALL_INDEX extends CallStrategy 
}

trait MyStrategy { def random(as: CallStrategy) } 

object MyStrategy extends MyStrategy 
{
   def random(as: CallStrategy) = { //Some code }
}

In another scala file I can access the CALL_INDEX using: CallStrategy.CALL_INDEX

But in java, I am not able to access it as follows:

StrategyUser.java

CallStrategy$ callStrategy = CallStrategy$.MODULE$;
MyStrategy$ myStrategy = MyStrategy$.MODULE$;
myStrategy.random(callStrategy); // compile time error

The above java code is giving compile time error:

The method random(CallStrategy) in the type MyStrategy$ is not applicable for the arguments (CallStrategy$)

I used the above java code by taking help from:

How do I "get" a Scala case object from Java?

UPDATE:

Please see the image below for clarification:

enter image description here

Even I added two more statements in the code and still the same compile time error:

enter image description here

Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185

2 Answers2

1
CallStrategy callStrategy = CallStrategy$.MODULE$.CALL_INDEX$.MODULE$

Your current code doesn't mention CALL_INDEX at all: it's equivalent to MyStrategy.random(CallStrategy) in Scala.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • Now talking about your statement: When I use `CallStrategy$.MODULE$.` and then press `Control + Space` then there is no drop down with `CALL_INDEX$`. Still I typed it. I wrote that complete statement and it is giving compile time error saying: `CALL_INDEX$ cannot be resolved or is not a field`. I am using `Eclipse IDE` which shows the methods to choose when we presses `Control + Space`. – sjain Dec 31 '15 at 09:50
  • `MyStrategy.scala` file is in the package: `com.test.user`. So I used the following import statement: import `com.test.user.CallStrategy$;`. I typed half statement `CallStrategy callStrategy = CallStrategy$.` and pressed the key `Control + Space` and the `CALL_INDEX$` was appearing in the drop down list. I chose it. Then again I pressed Control + Space. Now I got `MODULE$` from the drop down. I chose it. Complete statement now is: `CallStrategy callStrategy = CallStrategy$.CALL_INDEX$.MODULE$;`. Still compile time error: `cannot convert from CallStrategy$.CALL_INDEX$ to CallStrategy`. – sjain Dec 31 '15 at 10:09
  • I am not sure that despite of eclipse giving me the list to choose from when I press `Control + Space`, I got the compile time errors when I write these statements. However, I am not getting any error when I use: `CallStrategy$ callStrategy= CallStrategy$.MODULE$;`. – sjain Dec 31 '15 at 10:17
  • You need to get a `CallStrategy`, not a `CallStrategy$`. Are you sure you are getting the error in your previous comment with `CallStrategy callStrategy = CallStrategy$.CALL_INDEX$.MODULE$;`? This error message would make more sense if you had `CallStrategy callStrategy = CallStrategy$.CALL_INDEX$;`. – Alexey Romanov Dec 31 '15 at 12:56
  • `CallStrategy` is the type of `trait CallStrategy`; `CallStrategy$` is the type of `object CallStrategy`. So, as said above, it makes no sense to call `MyStrategy.random` with the object as its argument. – Alexey Romanov Dec 31 '15 at 13:11
  • Understood. But the problem is - I want to call the following method which is in scala file: `def random(as: CallStrategy)` as you see in my question. So the argument that it takes is of type of `trait CallStrategy`. Right? So in java class I have to call like that. Right? My problem is really same as this post: http://stackoverflow.com/questions/29629382/access-case-object-scala-from-java?rq=1 – sjain Dec 31 '15 at 13:31
  • As mentioned, please recheck `CallStrategy callStrategy = CallStrategy$.CALL_INDEX$.MODULE$;`. The error message doesn't make sense because this code doesn't try to `convert from CallStrategy$.CALL_INDEX$ to CallStrategy` at all. – Alexey Romanov Dec 31 '15 at 17:47
0

You should do something like below:

MyStrategy$.MODULE$.random(CallStrategy.CALL_INDEX$.MODULE$);

In your code you are trying to access the CALL_INDEX from CallStrategy object, rather, it's available in CallStrategy interface.

Update 1: (Full Code)

MyStrategy.scala:

trait CallStrategy

object CallStrategy {
  case object CALL_INDEX extends CallStrategy
}

trait MyStrategy {
  def random(as: CallStrategy)
}

object MyStrategy extends MyStrategy {
  def random(as: CallStrategy) = {
    println(as)
  }
}

StrategyUser.java:

public class StrategyUser {
    public static void main(String[] args) {
        MyStrategy$.MODULE$.random(CallStrategy.CALL_INDEX$.MODULE$);
    }
}

And FYI, I am using scala version '2.11.7' and java version '1.7.0_75'

James
  • 2,756
  • 17
  • 19
  • It is not able to recongnize CALL_INDEX$ as a field or a variable. Can you please provide the scala libraries to use because it seems that some configuration is incorrect. My project is having two files: One scala file and one java file. Java file is calling method of scala file. See the images attached in question for a clearer view and project structure. – sjain Jan 04 '16 at 10:31
  • I can successfully as mentioned in the code above without any problems. I am using Intellij and the mentioned scala and java versions. It could be a problem with eclipse. Can you provide your eclipse version and the sbt plugin version? – James Jan 04 '16 at 10:40
  • oh yes you are right. My eclipse Mars is giving compile time error at `CALL_INDEX$` from your above code but when I run `StrategyUser.java`, I get the OUTPUT. That means the problem is only eclipse being not able to recognize `CALL_INDEX$` as a field or a variable of scala. My eclipse scala version is also `2.11.7` and java version `1.8.0_65`. Also my sbt version is `sbt 0.13.8` – sjain Jan 04 '16 at 11:21
  • Also can you point out some good online resources to grasp Scala as I am new to it. Any recommendations are welcome. Thanks! – sjain Jan 04 '16 at 11:58
  • I found the following series of tutorials to be very useful: 1. [Neophyte's guide to scala](http://danielwestheide.com/scala/neophytes.html). And don't miss to checkout 2. [Martin's course in Coursera](https://class.coursera.org/progfun-003) – James Jan 04 '16 at 12:02