Implement Java Interface with Raw type from Scala

一个人想着一个人 提交于 2019-12-03 14:08:54

After some trial and error and looking at the error messages, I came up with this which compiles:

import org.sonar.api.batch._
import org.sonar.api.resources._ 

object D {
  type R = Resource[T] forSome {type T <: Resource[_ <: AnyRef]}
  type S[T] = Resource[T] forSome {type T <: Resource[_ <: AnyRef]}
}

class D extends Decorator {
  def decorate(r: D.R, context: DecoratorContext) {}
  //def decorate(r: D.S[_], context: DecoratorContext) {} // compiles too
  def shouldExecuteOnProject(project: Project) = true
}

I'm not sure whether it will allow you to implement what you need. I looked at Resource, and it could represent File which extends Resource<Directory> or sometimes be an erased (raw?) type that just extends Resource like for Directory.

Edit: thinking about it some more, the forSome can be eliminated - this compiles too:

def decorate(resource: Resource[_ <: Resource[_ <: AnyRef]],
             context: DecoratorContext) {
}

I have no idea about an answer, but if I write

def decorate(r: Resource[Resource[_]])

I get an error

type arguments [Resource[_]] do not conform to class Resource's type parameter bounds [PARENT <: Resource[_ <: AnyRef]]

Which seems wrong to me, because I would think the actual type bounds should be more like Resource[_ <: Resource[_ <: Resource[... ...]] (AnyRef wouldn't be appropriate as an upper bound).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!