How to compose a shapeless HList from Nat

I have an HList from Nat and I want to display it

object NatToString extends Poly1 {
    implicit def caseNat = at[Nat](_.toString)
}

val list = _5 :: _3 :: HNil
list.map(NatToString) 

This code does not compile or throw:

could not find the implicit value for the mapper parameter:
shapeless.ops.hlist.Mapper [Main.Nat_to_String.type, shapeless. :: [shapeless.Nat._5, shapeless. :: [shapeless.Nat._3, shapeless.HNil]] ]

But if I do the same with Int (or String, or List, etc.) instead of Nat, it works fine.

How can I match an HList from Nat?

+4
source share
1 answer

The problem is that Poly1.Caseit is not covariant in its type parameter. Consider the following:

trait Foo
trait Bar extends Foo

val foo = new Foo {}
var bar = new Bar {}

object fooIdentity extends Poly1 {
  implicit def caseFoo = at[Foo](identity)
}

fooIdentity(foo) , fooIdentity(bar) .

HList _5 _3. Nat, NatToString , - Nat.

, :

object NatToString extends Poly1 {
  implicit def caseNat[N <: Nat] = at[N](_.toString)
}

, Nat , - .

+5

All Articles