9

He,

How to call from Java a Scala case class which has an implicit paramter?

Scala:

object Loggable {
 case class RunUnit(val id : Integer) {
  override def toString() = id.toString()
 }
 case class Run(
  val id : Integer, val unit : RunUnit, val start : Long
 )(implicit db : DB) { ... }
}

Java:

public class JTest {
  public static void main(String[] args) {
    // works fine
    Loggable.RunUnit ru = new Loggable.RunUnit(4711);
    // how to add the implicit parameter???
    new Loggable.Run(new Integer(4711), ru, 0L);
  }
}

Thanks,

/nm

1

1 Answer 1

10

You would have to provide the implicit explicitly. Try this:

new Loggable.Run(new Integer(4711), ru, 0L, db);

I tried to use javap to see what the signature are, and on a top level case class, there is simply an extra parameter to the constructor.

Edit: using your code (with a dummy DB class):

scala> :javap Loggable$Run
[snip]
    public Loggable$Run(java.lang.Integer, Loggable$RunUnit, long, DB);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.