Search code examples
scalashapeless

Automatically transfer HList into a Record


My goal is to automatically transfer a HList into a Record on demand.

Note that the below code is written in Scala 2.13 and uses singleton types instead of Symbols.

import shapeless._
import shapeless.record._
import shapeless.labelled._

type BestBeforeDate = Record.`"day" -> Option[Int], "month" -> Option[Int], "year" -> Int`.T

object PolyToField extends Poly1 {
   implicit def mapA[A, K]: Case.Aux[A, FieldType[K, A]] = at[A](a => field[K][A](a))
}

val x: BestBeforeDate = (None :: None :: 12 :: HNil)
   .map(PolyToField)

I get this error:

type mismatch;
 found   : None.type with shapeless.labelled.KeyTag[Nothing,None.type] :: None.type with shapeless.labelled.KeyTag[Nothing,None.type] :: Int with shapeless.labelled.KeyTag[Nothing,Int] :: shapeless.HNil
 required: emergencymanager.commons.data.package.BestBeforeDate
    (which expands to)  Option[Int] with shapeless.labelled.KeyTag[String("day"),Option[Int]] :: Option[Int] with shapeless.labelled.KeyTag[String("month"),Option[Int]] :: Int with shapeless.labelled.KeyTag[String("year"),Int] :: shapeless.HNil

It seems like the second type parameter of the Poly1 function is inferred to Nothing instead of what I need.

How can I achieve this?


Solution

  • Firstly, you should specify with type ascription that None has type Option[...] (that's the reason why in Cats there are none[...] and .some, see also, item 9). Or use Option.empty[...].

    Secondly, you should use mapper with return type rather than standard shapeless.ops.hlist.Mapper

    trait MapperWithReturnType[HF, Out <: HList] extends Serializable {
      type In <: HList
      def apply(t: In): Out
    }
    
    object MapperWithReturnType {
      type Aux[HF, Out <: HList, In0 <: HList] = 
        MapperWithReturnType[HF, Out] { type In = In0 }
      def instance[HF, Out <: HList, In0 <: HList](f: In0 => Out): Aux[HF, Out, In0] = 
        new MapperWithReturnType[HF, Out] {
          override type In = In0
          override def apply(t: In0): Out = f(t)
        }
    
      implicit def hnilMapper[HF <: Poly]: Aux[HF, HNil, HNil] = instance(_ => HNil)
    
      implicit def hconsMapper[HF <: Poly, InH, InT <: HList, OutH, OutT <: HList](implicit
        hc : poly.Case1.Aux[HF, InH, OutH],
        mt : Aux[HF, OutT, InT]
      ): Aux[HF, OutH :: OutT, InH :: InT] = instance(l => hc(l.head) :: mt(l.tail))
    }
    
    implicit final class HListOps[L <: HList](l : L) extends Serializable {
      def mapWithReturnType[Out <: HList](f: Poly)(implicit 
        mapper: MapperWithReturnType.Aux[f.type, Out, L]
      ): Out = mapper(l)
    }
    
    val x = ((None: Option[Int]) :: (None: Option[Int]) :: 12 :: HNil)
        .mapWithReturnType[BestBeforeDate](PolyToField)
    
    // val x = (Option.empty[Int] :: Option.empty[Int] :: 12 :: HNil)
    //    .mapWithReturnType[BestBeforeDate](PolyToField)
    
    x: BestBeforeDate