Question

Suppose I have type alias defined in scala as

object Foo {
  type Bar = Option[String]
}

It looks like I cannot refer to alias in Java code like that (it simply complains cannot find symbol):

import Foo.*;

public class Cafebabe {
  void bar(Bar x) {
  //...
  }
}

I've tried static import as well.

(More specifically, I have java reflection code which I cannot change that needs to know parameter type and I need to feed Bar alias to it).

I know, I can create wrapper in Scala

class BarWrapper(value: Bar)

but maybe I'm missing some other way?

Était-ce utile?

La solution

Type aliases are only visible to the Scala compiler, and like generic types they don't appear anywhere in the JVM class files.

If you're in Java, you're stuck using the unaliased type Option[String] since javac has no way of knowing about the type alias Bar that you declared in your Scala code. Wherever you would have used Bar just use Option[String] (which is scala.Option<String> in Java) and it should work fine.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top