在Scala的Iso宏

时间:2014-07-11 17:35:35

标签: scala implicit-conversion scalaz scala-macros scalaz7

如果我想隐式地将两个对象从一个转换为另一个,那么无论如何使用像Iso宏这样的东西吗?

例如,如果我有这个:

implicit def listToMap[A, B](l: List[(A, B)]): Map[A, B] = l.toMap
implicit def mapToList[A, B](m: Map[A, B]): List[(A, B)] = m.toList

我想写一下:

implicit def[A, B] listMapIso = Iso[List[(A, B)], Map[A, B]] {_.toMap, _.toList}

注意:如下所述,我计划在我的Web框架中使用它,我将数据库模型转换为中间件/前端模型。

1 个答案:

答案 0 :(得分:4)

您似乎混淆了几个不同的概念。 Iso,隐式转换和宏都彼此完全不同。

我们当然可以为参数化类型定义等价的Iso,尽管语法变得有点麻烦:

import scalaz._, Scalaz._
case class BiIso[F[_, _], G[_, _]](left: F ~~> G,
  right: G ~~> F)

type PairList[A, B] = List[(A, B)]
val listToMap = new (PairList ~~> Map) {
  def apply[A, B](l: PairList[A, B]) = l.toMap
}
val mapToList = new (Map ~~> PairList) {
  def apply[A, B](m: Map[A, B]) = m.toList
}

val listMapIso = BiIso(listToMap, mapToList)

我们当然可以将这部分内容隐含起来,尽管这是一个正交的问题。我们可以隐含地构建BiIso:

implicit val listToMap = new (PairList ~~> Map) {
  def apply[A, B](l: PairList[A, B]) = l.toMap
}
implicit val mapToList = new (Map ~~> PairList) {
  def apply[A, B](m: Map[A, B]) = m.toList
}

implicit def biIso[F[_, _], G[_, _]](implicit left: F ~~> G, right: G ~~> F) =
  BiIso(left, right)

implicitly[BiIso[PairList, Map]]

我们可以将任何BiIso作为隐式转换,但我会建议反对它。唯一棘手的部分是正确引导类型推断。这是大多数情况,但由于某种原因,GAB参数没有推断(非常欢迎更正):

sealed trait BiAny[F[_, _]] {}
object BiAny {
  implicit def any[F[_, _]] = new BiAny[F] {}
}

sealed trait ApplyBiIso[FAB, GAB] {
  type A1
  type B1
  type F[_, _]
  type G[_, _]
  type Required = BiIso[F, G]

  val unapplyL: Unapply2[BiAny, FAB] {
    type A = A1; type B = B1;
    type M[C, D] = F[C, D]
  }
  val unapplyR: Unapply2[BiAny, GAB] {
    type A = A1; type B = B1;
    type M[C, D] = G[C, D]
  }

  def liftBI(bi: Required): Iso[FAB, GAB] =
    Iso({ fab: FAB =>
      val f: F[A1, B1] = Leibniz.witness(unapplyL.leibniz)(fab)
      val g: G[A1, B1] = bi.left(f)
      Leibniz.witness(Leibniz.symm[⊥, ⊤, GAB, G[A1, B1]](unapplyR.leibniz))(g): GAB
    },
      { gab: GAB =>
        val g: G[A1, B1] = Leibniz.witness(unapplyR.leibniz)(gab)
        val f: F[A1, B1] = bi.right(g)
        Leibniz.witness(Leibniz.symm[⊥, ⊤, FAB, F[A1, B1]](unapplyL.leibniz))(f): FAB
      }
    )
}

object ApplyBiIso {
  implicit def forFG[FAB, A2, B2, GAB, A3, B3](
    implicit u1: Unapply2[BiAny, FAB] { type A = A2; type B = B2 },
    u2: Unapply2[BiAny, GAB] { type A = A3; type B = B3 }) = new ApplyBiIso[FAB, GAB] {
    type A1 = A2
    type B1 = B2
    type F[C, D] = u1.M[C, D]
    type G[C, D] = u2.M[C, D]

    //Should do the conversion properly with Leibniz but I can't be bothered
    val unapplyL = u1.asInstanceOf[Unapply2[BiAny, FAB] {
      type A = A1; type B = B1;
      type M[C, D] = F[C, D]
    }]
    val unapplyR = u2.asInstanceOf[Unapply2[BiAny, GAB] {
      type A = A1; type B = B1;
      type M[C, D] = G[C, D]
    }]
  }
  type Aux[FAB, GAB, Required1] = ApplyBiIso[FAB, GAB] { type Required = Required1 }
  def apply[FAB, GAB](implicit abi: ApplyBiIso[FAB, GAB]): Aux[FAB, GAB, abi.Required] = abi
}

sealed trait AppliedBiIso[FAB, GAB] {
  val iso: Iso[FAB, GAB]
}
object AppliedBiIso {
  implicit def applyAndIso[FAB, GAB, Required1](
    implicit ap: ApplyBiIso.Aux[FAB, GAB, Required1],
    iso1: Required1) = new AppliedBiIso[FAB, GAB] {
    //Should do the conversion properly with Leibniz but I can't be bothered
    val iso = ap.liftBI(iso1.asInstanceOf[BiIso[ap.F, ap.G]])
  }
}

implicit def biIsoConvert[FAB, GAB](
  f: FAB)(implicit ap: AppliedBiIso[FAB, GAB]): GAB =
  ap.iso.left(f)

val map: Map[String, Int] = Map("Hello" -> 4)

val list: PairList[String, Int] =
  biIsoConvert[Map[String, Int], PairList[String, Int]](map)

我毫无疑问可以正常使用。

这仍然留下了宏,这也是一个或多或少正交的问题。我可以看到它们可能相关的一个地方是,如果不使用宏,它就不可能在scala中进行抽象。你想要一个等效的Iso适用于任何"形状"而不只是F[_, _]?对于一个宏来说,这将是一个很好的用例 - 虽然在我不羡慕任何试图实现它的人之前写过这种宏。