Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Generics question mark in Scala

Tags:

java

scala

I have an API with a signature in Java that return public Foo<?> getFoo()

Then when I try to consume that API from Scala, is complaining about ?.

val fooValue:Foo[_] = API.getFoo()

I also try with Any

val fooValue:Foo[Any] = API.getFoo()

Any idea how can I set the question mark type in Scala

like image 680
paul Avatar asked Sep 25 '26 08:09

paul


1 Answers

The following code compiles

Foo.java

public class Foo<T> {
}

API.java

public class API {
    public static Foo<?> getFoo() {
        return null;
    }
}

App.scala

object App {
  val fooValue: Foo[_] = API.getFoo()
}

Foo[?] is not valid Java syntax.

If the method is public static Foo<?> getFoo() you can call it like API.getFoo(). If it's public Foo<?> getFoo() you can call it like new API().getFoo().

Question mark (?) in Java corresponds to underscore (_) in Scala.

like image 157
Dmytro Mitin Avatar answered Sep 27 '26 23:09

Dmytro Mitin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!