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?
A type alias is usually used to simplify declaration for complex types, such as parameterized types or function types. Let's explore examples of those aliases and also look at some illegal implementations of type aliases.
In Java, Alias is used when reference, which is of more than one, is linked to the same object. The issue with aliasing is when a user writes to a particular object, and the owner for the several other references do not expect that object to change.
Non-value types capture properties of identifiers that are not values. For example, a type constructor does not directly specify a type of values. However, when a type constructor is applied to the correct type arguments, it yields a first-order type, which may be a value type.
Use the getClass Method in Scala The getClass method in Scala is used to get the class of the Scala object. We can use this method to get the type of a variable.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With