So, I want the common attribute to take a class with a companion object that inherits from a specific base class and refers to both the companion object and the class itself as a type parameter. In this way,
abstract class BaseModel[T] {
def all: Seq[T]
}
case class Customer(email: String, password: String)
object Customer extends BaseModel[Customer]
// This trait is my issue
trait BaseCrud[T] {
def table[T](f: T => String): String = {
T.all.map(f _).mkString("")
}
}
object Controller with BaseCrud {
def foo = table(_.email)
}
I had some solutions for this trait that were closer, but I redid it so you can see what I'm trying to do.
thank
UPDATE
So, I went with a solution from Frank below, but I managed to solve my original riddle. Although, in this case, the solution was a little ugly, I will include it here for completeness.
abstract class BaseModel[T] {
def all: Seq[T] = Seq()
}
case class Customer(email: String, password: String)
object Customer extends BaseModel[Customer]
trait BaseCrud[T, U <: BaseModel[T]] {
def table(f: T => String)(implicit obj: U): String = {
obj.all.map(f(_)).mkString("")
}
}
object Controller extends BaseCrud[Customer, Customer.type] {
implicit val model = Customer
def foo = table(_.email)
}
, , BaseCrud, BaseCrud.table Controller.model. . , Customer.type .