Compiler cannot find right implicits for shapeless

2019-05-10 02:19发布

问题:

Continuing the discussion about converting case classes and Map[String,Any], I faced some problems when I wanted to use it in a generic way, Here is the main discussion.

And when I want to use a generic method like following, compiler complains about the implicits:

import MapConvertor._
def convert[A](cc: A)= {
    cc.toMapRec
}

Here is the whole code that provides toMapRec:

import shapeless._, labelled.FieldType, record._

trait ToMapRec[L <: HList] { def apply(l: L): Map[String, Any] }

trait LowPriorityToMapRec {
  implicit def hconsToMapRec1[K <: Symbol, V, T <: HList](implicit
                                                          wit: Witness.Aux[K],
                                                          tmrT: ToMapRec[T]
                                                           ): ToMapRec[FieldType[K, V] :: T] =
    new ToMapRec[FieldType[K, V] :: T] {

      def apply(l: FieldType[K, V] :: T): Map[String, Any] =
        tmrT(l.tail) + (wit.value.name -> l.head)
  }
}

object ToMapRec extends LowPriorityToMapRec {
  implicit val hnilToMapRec: ToMapRec[HNil] = new ToMapRec[HNil] {
    def apply(l: HNil): Map[String, Any] = Map.empty
  }

  implicit def hconsToMapRec0[K <: Symbol, V, R <: HList, T <: HList](implicit
                                                                      wit: Witness.Aux[K],
                                                                      gen: LabelledGeneric.Aux[V, R],
                                                                      tmrH: ToMapRec[R],
                                                                      tmrT: ToMapRec[T]
                                                                       ): ToMapRec[FieldType[K, V] :: T] = new ToMapRec[FieldType[K, V] :: T] {
    def apply(l: FieldType[K, V] :: T): Map[String, Any] =
      tmrT(l.tail) + (wit.value.name -> tmrH(gen.to(l.head)))
  }
}

object MapConvertor {

  implicit class ToMapRecOps[A](val a: A) extends AnyVal {
    def toMapRec[L <: HList](implicit
                             gen: LabelledGeneric.Aux[A, L],
                             tmr: ToMapRec[L]
                              ): Map[String, Any] = tmr(gen.to(a))
  }

}

回答1:

You'll need to prove that the record that represents A has a ToMapRec instance, which you can do like this:

def convert[A, R <: HList](cc: A)(implicit
  gen: LabelledGeneric.Aux[A, R],
  tmr: ToMapRec[R]
) = tmr(gen.to(cc))

You can also introduce a new type class to clean this up a bit:

trait CcToMapRec[A] { def apply(a: A): Map[String, Any] }

object CcToMapRec {
  implicit def ccToMapRec[A, R <: HList](implicit
    gen: LabelledGeneric.Aux[A, R],
    tmr: ToMapRec[R]
  ): CcToMapRec[A] = new CcToMapRec[A] {
    def apply(a: A): Map[String, Any] = tmr(gen.to(a))
  }
}

And then your method just needs a single implicit parameter:

def convert[A](cc: A)(implicit ctmr: CcToMapRec[A]) = ctmr(cc)

But you'll still need to thread this instance through any method with a generic type that you intend to convert.