Hi all,
Reading Doobie examples (the Orm.scala
example) I don’t understand this Dao initialization snippet (I simplified it here to focus on the question):
trait Dao[A] {
type Key
// some methods
}
object Dao {
type Aux[A, K] = Dao[A] { type Key = K }
def apply[A](implicit ev: Dao[A]): Aux[A, ev.Key] = ev
object derive {
def apply[A, K] = new Partial[A, K]
class Partial[A, K] {
def apply[R <: HList, S <: HList](table: String, keyCol: String)(
implicit // some implicits that facilitate serialization/deserialization to/from JDBC
): Aux[A, K] =
new Dao[A] {
type Key = K
// implementation methods
}
}
and later:
// a little usage example
final case class Neighbor(name: String, age: Int)
object Neighbor {
implicit val dao: Dao.Aux[Neighbor, Int] =
Dao.derive[Neighbor, Int]("neighbor", "id")
implicit val show: Show[Neighbor] = Show.fromToString
}
val prog: ConnectionIO[String] = {
val dn = Dao[Neighbor]
import dn._
// rest of the program
}
I feel like this is a very elaborate way to say what amounts to new Dao[Neighbor]
but I am certain that I am missing something. What would that be? Is there a reason for this pattern, maybe even a name for it?
Thanks.