Java coder trying to use Scala here...
I have a generated Java builder that has a method
public com.yada.SomeEntity.Builder setSomeValue(java.lang.Integer value) {
...
}
From Scala, I can call this with an integer value or with null (and the field is deliberately defined to be nullable).
In my Scala code, I have, let's say
val someInput: Option[Int] = someMethod()
The problem is that none of these work
builder.setSomeValue(someInput)- obviously, because it's an Option[Int] and not an Integer.builder.setSomeValue(someInput.orNull)- becausesomeInput.orNullisAnyand not an Integerbuilder.setSomeValue(someInput.orElseGet(null)- that's also anAny
I can do this
builder.setSomeValue(if (someInput.isDefined) someInput.get else null)
Or alternatively, I can map something on someInput, but that breaks up the fluent style of the builder and just looks ugly.
Is there a pretty way to handle this in Scala?
builder.setSomeValue(someInput.map(Int.box).orNull)- You have to manually box the value. - Another alternative would bebuilder.setSomeValue(someInnput.fold(ifEmpty = null)(Int.box))