简体   繁体   English

从 Java 实例化一个 Scala 类,并使用构造函数的默认参数

[英]Instantiate a Scala class from Java, and use the default parameters of the constructor

I have a Scala class:我有一个Scala类:

class Foo(val x:String = "default X", val y:String = "default Y" ) 

I want to call it from Java , but using the default parameters我想从Java调用它,但使用默认参数

Passing null doesn't work (it assigns null , as expected)传递null不起作用(它按预期分配null

new Foo(null,null); //both are instantiated as null

This trick did work for me, but it's ugly, and I wonder if there is a better way:这个技巧确实对我有用,但它很丑陋,我想知道是否有更好的方法:

Scala斯卡拉

class Foo(val x:String = "default X", val y:String = "default Y" ) {
  def this(x:Object) = this()
}

Java爪哇

new Foo(null); //no matter what I pass it should work

However I would like to get rid of the constructor overload trick, and use a 0 param constructor但是我想摆脱构造函数重载技巧,并使用 0 param 构造函数

Is that possible?那可能吗?

It seems, there is no such way: https://issues.scala-lang.org/browse/SI-4278看来,没有这样的方法: https : //issues.scala-lang.org/browse/SI-4278

Issue : default no-args constructor should be generated for classes with all-optional arguments问题:应该为具有全可选参数的类生成默认的无参数构造函数
... ...

Lukas Rytz : in respect of language uniformity we decided not to fix this one - since it's a problem of interoperability with frameworks, we think it should not be fixed at the language level. Lukas Rytz :关于语言统一性,我们决定不修复这个问题——因为这是与框架的互操作性问题,我们认为不应该在语言级别修复。

workarounds: repeat a default, or abstract over one, or put one default int the zero-argument constructor解决方法:重复一个默认值,或抽象一个,或将一个默认值放入零参数构造函数

Then Lukas proposes the same solution as you found:然后 Lukas 提出了与您发现的相同的解决方案:

class C(a: A = aDefault, b: B = C.bDefault) {
  def this() { this(b = C.bDefault) }
}
object C { def bDefault = ... }

// OR

class C(a: A = aDefault, b: B) {
  def this() { this(b = bDefault) }
}

There is a solution, please check out section "Default Arguments" from article: https://lampwww.epfl.ch/~michelou/scala/using-scala-from-java.html有一个解决方案,请查看文章中的“默认参数”部分: https : //lampwww.epfl.ch/~michelou/scala/using-scala-from-java.html

It's possible to invoke both constructors and methods by passing the appropriate positional argument from java using .$default$[number] format.可以通过使用 .$default$[number] 格式从 java 传递适当的位置参数来调用构造函数和方法。

Here scope is as follows:这里的范围如下:

  • Class constructor: << ClassName >>.init$default$1 for the value of the first arguments default value set in the constructor definition default$2 for second arguments default value and so on.类构造函数: << ClassName >>.init$default$1 为构造函数定义中设置的第一个参数默认值 default$2 为第二个参数默认值等。
  • Method call: object.methodName$default$1 to resolve the methods first parameters default value assigned in the method signature etc.方法调用:object.methodName$default$1 解析方法签名中分配的方法第一个参数默认值等。

Example:例子:

import za.co.absa.spline.core.SparkLineageInitializer;
SparkLineageInitializer.SparkSessionWrapper lineage = SparkLineageInitializer.SparkSessionWrapper(spark);
lineage.enableLineageTracking(lineage.enableLineageTracking$default$1());

For this example the maven dependency is: groupId: za.co.absa.spline artifactId: spline-core version: 0.3.1对于此示例,maven 依赖项是: groupId: za.co.absa.spline artifactId: spline-core version: 0.3.1

More generally if you have a Scala class with default args and you want to instantiate in Java overriding 0, 1 or more of the defaults without having to specify all, consider extending the Scala API to include a Builder in the companion object.更一般地说,如果您有一个带有默认参数的 Scala 类,并且您想在 Java 中实例化覆盖 0、1 或多个默认值而不必指定所有,请考虑扩展 Scala API 以在伴随对象中包含一个 Builder。

case class Foo(
  a: String = "a",
  b: String = "b",
  c: String = "c"
)

object Foo {
  class Builder {
    var a: String = "a"
    var b: String = "b"
    var c: String = "c"
    def withA(x: String) = { a = x; this }
    def withB(x: String) = { b = x; this }
    def withC(x: String) = { c = x; this }
    def build = Foo(a, b, c)
  }
}

public class App {
    public static void main(String[] args) {
        Foo f = new Foo.Builder()
            .withA("override a")
            .build();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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