How to implicitly figure out the type at the head of a shapeless HList

后端 未结 1 421
误落风尘
误落风尘 2021-01-27 07:39

Lets say I have the following:

case class TestField(value: String)
case class TestField2(value: String)

implicit class ProductExtensions[T <: Product](val va         


        
相关标签:
1条回答
  • 2021-01-27 08:10

    Here is generalized version

    implicit class ProductExtensions[T <: Product, L <: HList](val value: T) extends AnyVal {
      def mapTo[R <: Product](implicit tGen: Generic.Aux[T, L], rGen: Generic.Aux[R, L]): R = rGen.from(tGen.to(value))
    }
    

    The Type Astronaut’s Guide to Shapeless. 6.3 Case study: case class migrations https://books.underscore.io/shapeless-guide/shapeless-guide.html#sec:ops:migration


    New version

    import shapeless.ops.hlist.IsHCons
    
    implicit class ProductExtensions[T <: Product, L <: HList, U, L1 <: HList](val value: T) extends AnyVal {
      def mapTo[R <: Product](implicit 
                              tGen: Generic.Aux[T, L], 
                              rGen: Generic.Aux[R, L], 
                              isHCons: IsHCons.Aux[L, U, L1], 
                              o: OtherImplicit[U]
                             ): R = rGen.from(tGen.to(value))
    }
    

    4.3 Chaining dependent functions https://books.underscore.io/shapeless-guide/shapeless-guide.html#sec:type-level-programming:chaining

    Scala shapeless Generic.Aux implicit parameter not found in unapply

    0 讨论(0)
提交回复
热议问题