Using Scala type aliases from Java code

前端 未结 1 2132
有刺的猬
有刺的猬 2020-12-20 14:59

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 cod

1条回答
  •  盖世英雄少女心
    2020-12-20 15:10

    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 in Java) and it should work fine.

    0 讨论(0)
提交回复
热议问题