JayZee
JayZee

Reputation: 860

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

Upvotes: 7

Views: 4518

Answers (2)

Landei
Landei

Reputation: 54574

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 
}

Upvotes: 5

agilesteel
agilesteel

Reputation: 16859

abstract class CommonObjectInfo
class ConcreteObject extends CommonObjectInfo

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

Upvotes: 9

Related Questions