我有一个用Java定义的protobuf方法
public Builder addAllTensor(
Iterable<? extends Float> values)我用val a: ArrayList[Float]调用Scala中的方法,它在代码检查期间是正常的,但是在构建过程中出错。
type mismatch;
found : java.util.ArrayList[scala.Float]
required: Iterable[_ <: java.lang.Float]但是用a.toIterable调用它会导致代码检查中的错误(在IntelliJ IDEA中)
Required: Iterable[_ <: Float], found: Iterable[Float] (这怎么可能是类型错配……)只是好奇为什么..。
发布于 2021-12-08 06:49:45
问题在于scala.Float与Java的Float。
自动转换不会在集合中发生,您必须以某种方式创建Java的Float集合。
(编辑)更多的上下文(感谢@Suma):Scala的Float对应于float,而Java容器只能包含Float,而不能包含float,因为在Java原语中需要装箱才能参与泛型。
https://stackoverflow.com/questions/70269416
复制相似问题