문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top