【发布时间】:2016-09-08 15:28:26
【问题描述】:
我正在用 scala 编写,我正在处理一个 Java API,它返回一个
List<? extends IResource>,其中IResource 是通用父接口 (the details, if it helps)。
我正在尝试将 IResource 添加到该方法返回的列表中,但我无法编译我的代码(Patient is a java class which implements IResource,getContainedResources 返回 @987654329 @):
这是我的原始代码
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
请注意,我正在尝试将我输入的patientASResource 添加到界面IResource。尝试添加 patient(实现接口的类)会出现更严重的错误消息。
我尝试过的其他事情:
//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)
^
【问题讨论】:
标签: scala