来自Scala的Java通配符通用类型Interop

时间:2016-09-08 00:54:21

标签: scala

我在scala中写作,而且我正在处理一个返回a的Java API IResource,其中IResource是通用父接口(default for the argument to be optional)。

我尝试将Patient添加到该方法返回的列表中,但我无法获取要编译的代码(IResource the details, if it helps {{ 1}},is a java class which implements返回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)
 ^

也不起作用,它会返回如下内容:

OdGeLine2d ln(OdGePoint2d::kOrigin, OdGeVector2d::kXAxis);
const double dOffsetDistance = 100.0;

OdGeVector2d vOffset = ln.direction().perpVector(); //ccw rotation
vOffset.normalize();
vOffset *= dOffsetDistance;

ln.transformBy( OdGeMatrix2d::translation(vOffset) );

1 个答案:

答案 0 :(得分:1)

问题不在于互操作。这绝对不应该编译,也不应该等效的Java代码。

$.ajax({ url: path1,data: data }).done(function Ajax1Succeeeded() { console.log("First done"); $.ajax({ url: path2, data: data }).done(function Ajax2Succeeeded() { console.log("Both done");  }) }).fail(function Ajax1Error(){ console.log("First resulted in error"); }); 表示它可以是var ajax1 = $.ajax({url: path1}); var ajax2 = $.ajax({url: path2}); Promise.all([ajax1, ajax2]).then(function(response1, response2){ console.log("Both ajax call were successful"); }, function(){ console.log("There was an error"); }); List<? extends IResource>List<IResource>List<Patient>等等,您不知道哪个。因此,不允许在此类列表中添加List<SomeSubclassOfPatient>(或上传后的List<SomeOtherResourceUnrelatedToPatient>)。

如果您知道在特定情况下Patient IResource返回entryentry.getResource.getContained.getContainedResources,您应该尝试通过指定静态确保这一点当覆盖List[IResource]时。如果这是不可能的,最后的办法是施放:

List[Patient]

重申一下:如果可能的话,你应该避免这种情况。

相关问题