I have a Scala class:
class Foo(val x:String = "default X", val y:String = "default Y" )
I want to call it from Java, but using the default parameters
Passing null doesn’t work (it assigns null, as expected)
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
Is that possible?
It seems, there is no such way: https://issues.scala-lang.org/browse/SI-4278
Then Lukas proposes the same solution as you found: