Search code examples
scalashapelesshlist

Concatenating HList Types in Case Class Definition


I have three HLists A, B and R. Within a case class definition I want to enforce a relationship between those three HLists.

The code looks like this:

trait Base[A <: HList]    
case class Concat[A <: HList, B <: HList, R <: HList](a: A, b: B) extends Base[R]

Is it possible to enforce that R is a concatenation of the HLists A and B? Or is there a way that I do not need the type R but still enforce that the type parameter of Base is a concatenation of A and B?


Solution

  • You want Prepend:

    case class Concat[A <: HList, B <: HList, R <: HList](a: A, b: B)(implicit p: Prepend.Aux[A, B, R]) extends Base[R]
    

    And a useful companion method for constructing without specifying R

    def concat[A <: HList, B <: HList](a: A, b: B)(implicit p: Prepend[A, B]): Concat[A, B, p.Out] = Concat(a, b)(p)