Biorąc pod uwagę typeclass Printer
z zależną typu Show[A]
:Jak zakodować tę metodę za pomocą niejawnej grupy parametrów zawierającej typ zależny?
trait Printer {
type Show[A]
def show[A](x: A)(implicit z: Show[A]): String
}
object Printer {
// the intent here is this is the dumb fallback
// and a user can have a better alternative in scope
implicit val dumbPrinter: Printer = new Printer {
type Show[A] = DummyImplicit
def show[A](x: A)(implicit z: DummyImplicit): String = x.toString
}
}
Jak mogę kodować tą metodą:
def r[A](x: A)(implicit printer: Printer, show: printer.Show[A]): String =
printer.show(x)(show)
Próbowałem dostosować kod do pracy w @ MilesSabin na Gist https://gist.github.com/milessabin/cadd73b7756fe4097ca0 i @ Blog TravisBrown: https://meta.plasm.us/posts/2015/07/11/roll-your-own-scala/, ale nie mogę znaleźć kodowania, które działa.
Czy nie jest to problem braku wielu ukrytych parametrów sekcji w Scala? Sądzę, że Miles Sabin zrobił ostatnio SI, ale nie mogę go znaleźć. (edytuj: widzę, że jest to już wspomniane w tekście, do którego się odnosisz) – Haspemulator
@ Haspemulator Poprawnie. –
Nie oglądałem przykładów. Ale co powiesz na zrobienie nowego typu, który będzie krotkiem 'Printer' i' Show [A] ':' case class Printable [A] (printer: Printer) (niejawny show: printer.Show [A]) '. Następnie spraw, aby 'r' wymagało' Printable [A] '. – ziggystar