Iso Macro in Scala

If I want to implicitly convert two objects from one to another, is there a way to do this using something like an Iso macro?

For example, if I have this:

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

I just want to write:

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

Note. As indicated below, I plan to use this in my web environment, where I convert my database models to middleware / front-end models.

+4
source share
1 answer

You seem to be mixing a few different concepts. Iso, implicit conversions and macros are completely different from each other.

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[_, _]? - , , .

+4

Source: https://habr.com/ru/post/1548104/


All Articles