I have some scala traits with same self-type declared as following.
trait BookDbModule {
self: DbConfig => // Abstract this to a parent trait
/* ... */
}
trait AuthorDbModule {
self: DbConfig => // Abstract this to a parent trait
/* ... */
}
I am trying to abstract the self-type declaration to a parent trait such that each of these traits does not have to define self-type. I tried the following.
trait DbModule {
self: DbConfig =>
// Some common DbModule methods
}
// !!! Illegal Inheritance, self-type BookDbModule does not conform to DbConfig
trait BookDbModule extends DbModule {
// What needs to be used instead of extends?
/* ... */
}
// !!! Illegal Inheritance, self-type AuthorDbModule does not conform to DbConfig
trait AuthorDbModule extends DbModule {
// What needs to be used instead of extends?
/* ... */
}
The error messages Illegal Inheritance
makes sense to me as BookDbModule
does not extend DbConfig
.
Is there any way in Scala to enforce self-type of children traits in a parent trait?
Update: It seems like the question is little bit confusing.
What I want to achieve is, I want to omit necessity to set self-type for BookDbModule
and AuthorDbModule
by extending (or any other scala feature) the parent trait DbModule
while has the self-type DbConfig
.
So, basically, I am looking for a way to make children traits (BookDbModule
and AuthorDbModule
) be extended by only those classes with DbConfig
by declaring self-type in parent DbModule
but not in those children traits.
// This works but is there any way to omit necessity to write
// self: DbConfig =>
trait AuthorDbModule extends DbModule {
self: DbConfig =>
/* ... */
}
Please let me know if it is still confusing.
Thank You!