开发者

bounded generics in Scala (as <E extends MyClass> in Java )

I'm migrating an app from java to Scala. In java I have somethng like

abstract class CommonObjectInfo{//...}
class ConcreteObject extends CommonObjectInfo{//...}
开发者_运维技巧
abstract class AbstractWrapper<E extends CommonObjectInfo>{//...} 
class ConcreteWrapper extends CommonObjectInfo<ConcreteObject>{//...} 

How can I express formally the "wrappers" objects in Scala? I


abstract class CommonObjectInfo
class ConcreteObject extends CommonObjectInfo

abstract class AbstractWrapper[E <: CommonObjectInfo]
class ConcreteWrapper extends AbstractWrapper[ConcreteObject]


The usual solution is the one from agilesteel, but sometimes it's useful to pull the type information "inside" the class (especially when the type in question is considered to be an implementation detail):

abstract class CommonObjectInfo

class ConcreteObject extends CommonObjectInfo

abstract class AbstractWrapper{
 type objectInfo <: CommonObjectInfo
}

class ConcreteWrapper {
  type objectInfo = ConcreteObject 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜