How to declare optional basic type in scala e.g. Option[Int] so that the type gets seen as Option<Integer> from java?

101 views Asked by At

Consider the following scala class:

class Demo {
  def mandatoryInt: Int = 42

  def optInt: Option[Int] = Some(1)

  def optString: Option[String] = Some("demo")
}

When its methods are accessed from Java code, optString has type Option<String>. However, method optInt has type Option<Object>.

I guess, it's because basic types are mapped to primitive java types. E.g. mandatoryInt has type int in Java. And Option<int> is not a valid java type.

Does anyone have an idea how to declare optional basic type in scala so that it gets normally inferred in java (i.e. Option[Int] to be seen as Option<Integer>)?

(Scala version 2.12.10, although I doubt that it matters much for my question)

0

There are 0 answers