简体   繁体   中英

Java calling Scala case class w/ implicit parameter?

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM