Witness type derivation issue

203 views Asked by At

I want to build general solution for field removing from case classes. Using this trick I built this working code:

  implicit class SemiGenericIgnoringOps[T](t: T) {

    def ignoring[TRepr <: HList, 
                 V, 
                 TargetRepr <: HList, 
                 H <: HList](k: Witness)
                            (implicit
                             gen: LabelledGeneric.Aux[T, TRepr],
                             rem: Remover.Aux[TRepr, k.T, (V, TargetRepr)],
                             upd: Updater.Aux[TargetRepr, FieldType[k.T, V], H],
                             ali: Align[H, TRepr]
    ): SemiGeneric.Aux[T, TargetRepr] = new SemiGeneric[T] {
      type Repr = TargetRepr
      def convert: TargetRepr = gen.to(t) - k
    }
  }

I want to replace single k: Witness with HList of Witnesss. But even adding a generic Witness type parameter cause the compilation error: can't find implicit value for Remover.

  implicit class SemiGenericIgnoringOps[T](t: T) {

    def ignoring[TRepr <: HList,
                 V,
                 TargetRepr <: HList,
                 H <: HList,
                 W <: Witness](w: W) // added type parameter
                              (implicit
                               gen: LabelledGeneric.Aux[T, TRepr],
                               rem: Remover.Aux[TRepr, w.T, (V, TargetRepr)],
                               upd: Updater.Aux[TargetRepr, FieldType[w.T, V], H],
                               ali: Align[H, TRepr]
    ): SemiGeneric.Aux[T, TargetRepr] = new SemiGeneric[T] {
      type Repr = TargetRepr
      def convert: TargetRepr = gen.to(t) - w
    }
  }

It seems that compilator can't derive Witness.T. Trick with Witness.Aux[R] doesn't help. How to overcome this problem?

1

There are 1 answers

2
Dmytro Mitin On BEST ANSWER

On contrary to the answer you referred to, you don't need Updater and Align since your convert works with HList / record.

The following code works:

  import shapeless.ops.record.Remover
  import shapeless.{::, HList, HNil, LabelledGeneric, Witness}

  trait GenericAllKeysRemover[A <: Product, K <: HList] {
    type Out <: HList
    def apply(a: A): Out
  }

  object GenericAllKeysRemover {
    type Aux[A <: Product, K <: HList, Out0 <: HList] = GenericAllKeysRemover[A, K] { type Out = Out0 }
    def instance[A <: Product, K <: HList, Out0 <: HList](f: A => Out0): Aux[A, K, Out0] = new GenericAllKeysRemover[A, K] {
      override type Out = Out0
      override def apply(a: A): Out = f(a)
    }

    def apply[A <: Product, K <: HList](implicit genericAllKeysRemover: GenericAllKeysRemover[A, K]): Aux[A, K, genericAllKeysRemover.Out] = genericAllKeysRemover

    implicit def mkGenericAllKeysRemover[A <: Product, L <: HList, K <: HList, Out <: HList](implicit
      labelledGeneric: LabelledGeneric.Aux[A, L],
      allKeysRemover: AllKeysRemover.Aux[L, K, Out]): Aux[A, K, Out] =
      instance(a => allKeysRemover(labelledGeneric.to(a)))
  }

  trait AllKeysRemover[L <: HList, K <: HList] {
    type Out <: HList
    def apply(l: L): Out
  }

  object AllKeysRemover {
    type Aux[L <: HList, K <: HList, Out0 <: HList] = AllKeysRemover[L, K] { type Out = Out0 }
    def instance[L <: HList, K <: HList, Out0 <: HList](f: L => Out0): Aux[L, K, Out0] = new AllKeysRemover[L, K] {
      override type Out = Out0
      override def apply(l: L): Out0 = f(l)
    }

    def apply[L <: HList, K <: HList](implicit allKeysRemover: AllKeysRemover[L, K]): Aux[L, K, allKeysRemover.Out] =
      allKeysRemover

    implicit def mkAllKeysRemover[L <: HList]: Aux[L, HNil, L] = instance(identity)
    implicit def mkAllKeysRemover1[L <: HList, H, T <: HList, V, L_removeH <: HList, Out <: HList](implicit
      remover: Remover.Aux[L, H, (V, L_removeH)],
      allKeysRemover: Aux[L_removeH, T, Out]): Aux[L, H :: T, Out] =
      instance(l => allKeysRemover(remover(l)._2))
  }

  case class MyClass(i: Int, s: String, b: Boolean)

  GenericAllKeysRemover[MyClass, Witness.`'s`.T :: Witness.`'b`.T :: HNil].apply(MyClass(1, "a", true)) //1 :: HNil