Java调用带有隐式参数的Scala案例类? [英] Java calling Scala case class w/ implicit parameter?

查看:55
本文介绍了Java调用带有隐式参数的Scala案例类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他,

如何从 Java 调用具有隐式参数的 Scala 案例类?

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

斯卡拉:

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

谢谢,

/纳米

推荐答案

您必须明确提供隐式.试试这个:

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

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

我尝试使用 javap 来查看签名是什么,并且在顶级案例类中,构造函数只有一个额外的参数.

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.

使用您的代码(使用虚拟数据库类):

using your code (with a dummy DB class):

scala> :javap Loggable$Run
[snip]
    public Loggable$Run(java.lang.Integer, Loggable$RunUnit, long, DB);
}

这篇关于Java调用带有隐式参数的Scala案例类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆