使用 Shapeless + LabelledGenerics 一般将 class 变形为另一个

Generically morph a class into another using Shapeless + LabelledGenerics

我知道使用 Shapeless 我可以做这样的事情:

  import shapeless._, syntax.singleton._, record._

  case class Foo(x: Int, y: String)
  case class RichFoo(x: Int, y: String, z: Double)

  def makeRich(foo: Foo): RichFoo = {
    val x = ('z ->> 0.9)
    val repr = LabelledGeneric[Foo].to(foo) + x
    LabelledGeneric[RichFoo].from(repr)
  }

  val a = Foo(1, "hello")
  val b = makeRich(a)

现在我想写一个通用的方法来做到这一点:

trait Morph[A, B, AR, BR] {
  def apply(a: A)(f: AR => BR): B
}

object Morph {
  implicit def genericMorph[A, B, AR, BR](implicit genA: LabelledGeneric.Aux[A, AR], genB: LabelledGeneric.Aux[B, BR]): Morph[A, B, AR, BR] =
    new Morph[A, B, AR, BR] {
      override def apply(a: A)(f: AR => BR) = genB.from(f(genA.to(a)))
    }

  implicit class Syntax[A](a: A) {
    def morph[AR, BR, B](f: AR => BR)(implicit morph: Morph[A, B, AR, BR]): B =
      morph(a)(f)
  }
}

但是,现在使用起来很不方便?

  val a = Foo(1, "hello")
  a.morph[???, ???, RichFoo](_ + ('z ->> 0.9))

设计这个 API 的更好方法是什么?

我试过这样的事情:

  implicit class Syntax[A](a: A) {
    def morphTo[B] = new {
      def using[AR <: HList, BR <: HList](f: AR => BR)(implicit morph: Morph[A, B, AR, BR]): B =
        morph(a)(f)
    }
  }

a.morphTo[RichFoo].using(_ :+ ('z ->> 0.9))

但实际上并没有用

有两个限制阻止类型推断在您的示例中以您想要的方式工作(顺便说一句,这两个限制都与 shapeless 无关):

  1. 当前 scalac 明确指定类型参数是全有或全无。但是您只想指定 B 剩下的要推断。 Currying 是解决此问题的一种方法。所以你的尝试是在正确的轨道上,但没有考虑到 2.

  2. 方法参数的类型推断一次从左到右一个参数列表。但是您想根据最后出现的 morph 的类型推断 f 的类型,因为它是隐式的。这里的解决方案是... 再次柯里化.

所以从 1. 和 2. 可以看出,你必须 咖喱两次:

implicit class Syntax[A](a: A) {
  def morphTo[B] = new {
    def by[AR <: HList, BR <: HList](implicit morph: Morph[A, B, AR, BR]) = new {
      def using(f: AR => BR): B = morph(a)(f)
    }
  }
}

a.morphTo[RichFoo].by.using(_ :+ ('z ->> 0.9))

对于 1,有一个替代解决方案。-使用虚拟参数来指定类型参数B

trait To[-A]
object To {
  private val instance = new To[Any] { }
  def apply[A]: To[A] = instance
}

implicit class Syntax[A](a: A) {
  def morph[B, AR <: HList, BR <: HList](to: To[B])(
    implicit morph: Morph[A, B, AR, BR]
  ) = new {
    def using(f: AR => BR): B = morph(a)(f)
  }
}

a morph To[RichFoo] using (_ :+ ('z ->> 0.9))

关于如何在 Dotty 中解决这些问题的未来参考

  1. 您已经可以部分指定类型参数:a.morph[B = RichFoo]
  2. 正在进行更一致的隐式参数语法的工作:lampepfl/dotty#1260

编辑: 通常将依赖于其他类型的类型定义为类型成员是个好主意:

trait Morph[A, B] {
  type AR
  type BR
  def apply(a: A)(f: AR => BR): B
}

object Morph {
  type Aux[A, B, AR0, BR0] = Morph[A, B] {
    type AR = AR0
    type BR = BR0
  }

  implicit def genericMorph[A, B, AR0, BR0](
    implicit genA: LabelledGeneric.Aux[A, AR0], genB: LabelledGeneric.Aux[B, BR0]
  ): Aux[A, B, AR0, BR0] = new Morph[A, B] {
    type AR = AR0
    type BR = BR0
    def apply(a: A)(f: AR => BR) = genB.from(f(genA.to(a)))
  }

  implicit class Syntax[A](a: A) {
    def morphTo[B](implicit morph: Morph[A, B]) = new {
      def using(f: morph.AR => morph.BR) = morph(a)(f)
    }
  }
}
import shapeless._, syntax.singleton._, record._

case class Foo(x: Int, y: String)
case class RichFoo(x: Int, y: String, z: Double)

class Morph[A, B, AR](a: A, genA: LabelledGeneric.Aux[A, AR]) {
  def apply[BR](f: AR => BR)(implicit genB: LabelledGeneric.Aux[B, BR]) = genB.from(f(genA.to(a)))
}

implicit class Syntax[A, AR](val a: A)(implicit genA: LabelledGeneric.Aux[A, AR]) {
  def morph[B]: Morph[A, B, AR] = new Morph(a, genA)
}

val a = Foo(1, "hello")
a.morph[RichFoo](_ + ('z ->> 0.9)) // => RichFoo(1,hello,0.9)

根据@g.krastev的回答,我采用了这种 DSL 方法:

import shapeless._, syntax.singleton._, record._, ops.hlist._

case class Morph[A, AR](a: A)(implicit reprA: LabelledGeneric.Aux[A, AR]) {
  def to[B] = new {
    def apply[BR](f: AR => BR)(implicit reprB: LabelledGeneric.Aux[B, BR]): B =
      reprB.from(f(reprA.to(a)))
  }
}

然后我们可以这样使用它:

val a = Foo(1, "hello")
val b = Morph(a).to[RichFoo](_ + ('z ->> 0.9)) // => RichFoo(1,hello,0.9)

我们也可以让它像这样处理

case class Morph[A, AR](a: A)(implicit reprA: LabelledGeneric.Aux[A, AR]) {
  def to[B] = new {       
    def apply[BR <: HList, BR2 <: HList](f: AR => BR2)(implicit reprB: LabelledGeneric.Aux[B, BR], align: Align[BR2, BR]): B =
      reprB.from(align(f(reprA.to(a))))
  }
}