Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java calling Scala case class w/ implicit parameter?

Tags:

java

scala

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

like image 363
nemron Avatar asked Oct 18 '11 14:10

nemron


1 Answers

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);
}
like image 199
huynhjl Avatar answered Sep 20 '22 02:09

huynhjl