Java/Scala Required: Iterable[_ <: Float], found: Iterable[Float]

Viewed 161

I have a protobuf method in Java with definition

public Builder addAllTensor(
          Iterable<? extends Float> values)

I call the method in Scala with a val a: ArrayList[Float], its ok during code check, but error during build

type mismatch;
 found   : java.util.ArrayList[scala.Float]
 required: Iterable[_ <: java.lang.Float]

But calling it with a.toIterable it raises error in code check(in IntelliJ IDEA)

Required: Iterable[_ <: Float], found: Iterable[Float] 

(How could this be type mismatch...) Just wonder why...

1 Answers

The issue is with the scala.Float versus Java's Float.

Automatic conversion doesn't happen inside collections, you have to create somehow a collection of Java's Float.

(EDIT) More context (thanks @Suma): Scala's Float corresponds to Java's float, and Java containers can contain only Float, not float, because in Java primitives need to be boxed to participate in generics.

Related