我有以下类/特征设置:
class RestService extends EntityReader[UserEntity]
with EntityReaderExtension {
def serveSomething() = {...}
}
trait EntityReader[EntityType <: StoredEntity] extends RestHelper {
protected def read(id:UUID): Option[EntityType] = {
// Read by id
}
}
trait EntityReaderExtension {
def serveExtensionMethod(id:UUID) = {
// val entity = read(id) match {...}
// copy values to other entity and serve
}
}
有没有办法可以在Trait EntityReaderExtension中使用trait EntityReader中的方法而无需扩展?
答案 0 :(得分:2)
我认为你想要的是explicitly typed self reference。
trait EntityReaderExtension { self: EntityReader[_] =>
def serveExtensionMethod(id:UUID) = {
val entity = self.read(id) match {...}
}
}