我正在寫scala,我正在處理一個Java API,它返回一個 List<? extends IResource>
,其中IResource
是一個通用的父接口(the details, if it helps)。Java通配符類型互操作從斯卡拉
我想一個IResource
添加到由該方法返回的名單,但我不能讓我的代碼編譯(Patient
is a java class which implementsIResource
,並且getContainedResources返回List<? extends IResource>
):
這裏是我的原代碼
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)
^
感謝您的評論幫助我意識到我的問題,這是我不瞭解存在類型/ java的通配符。 – user2741287