以下に概説するように、クラス階層でデータベースストレージを管理する一連のクラスがあり、ケースクラスがコンパニオンオブジェクトの親クラスの保護されたメソッドにアクセスできるようにしたいと考えています:
class TableBase[T] {
protected def insert(...):T {...}
protected def update(...) {...}
// Other "raw" CRUD-methods that I don't want the
// world to have access to
}
object User extends TableBase[User] {
}
case class User(id:Int, email:String) {
// But here it would be really useful to access the "raw" CRUD methods:
def changeEmail(newEmail:String) = User.update(...)
}
唯一の問題は、User (クラス) が TableBase からの継承チェーンに含まれていないため、User.changeEmail での User.update の呼び出しが不正であることです。
method update in class TableBase cannot be accessed in object models.User
Access to protected method update not permitted because enclosing class
class User in package models is not a subclass of class TableBase in package
models where target is defined
このタイプの呼び出しを許可する (便利な) 方法はありますか?
現時点では、changeEmail タイプの関数をシングルトンに移動する必要があります。これにより、呼び出しコードがかなり冗長になります。または、メソッド シグネチャを複製する必要があります。