来自Scala的Java通配符泛型类型互操作
我正在用scala编写,我正在处理一个Java API,它返回一个来自Scala的Java通配符泛型类型互操作,scala,Scala,我正在用scala编写,我正在处理一个Java API,它返回一个 List问题不在于互操作。这绝对不应该编译,等价的Java代码也不应该编译 list谢谢你的评论帮助我认识到了我的问题,那就是我不理解存在类型/java的通配符。 val patient = new Patient() patient.setId(ID) val patientASResource: IResource = patient entry.getResource.getContained.getContainedRe
List问题不在于互操作。这绝对不应该编译,等价的Java代码也不应该编译
list谢谢你的评论帮助我认识到了我的问题,那就是我不理解存在类型/java的通配符。
val patient = new Patient()
patient.setId(ID)
val patientASResource: IResource = patient
entry.getResource.getContained.getContainedResources.add(patient)
type mismatch;
found : patientASResource.type (with underlying type ca.uhn.fhir.model.api.IResource)
required: ?0 where type ?0 <: ca.uhn.fhir.model.api.IResource
entry.getResource.getContained.getContainedResources.add(patientASResource)
^
one error found
//From what I understand of "Java wildcards" per here: http://stackoverflow.com/a/21805492/2741287
type Col = java.util.Collection[_ <: IResource]
val resList: Col = entry.getResource.getContained.getContainedResources
val lst: Col = asJavaCollection(List(patient))
resList.addAll(lst)
type mismatch
found : java.util.Collection[_$1(in method transformUserBGs)] where type _$1(in method transformUserBGs) <: ca.uhn.fhir.model.api.IResource
required: java.util.Collection[_ <: _$1(in type Col)]
resList.addAll(lst)
^
entry.getResource.getContained.
getContainedResources.asInstanceOf[java.util.List[IResource]].add(patient)