How to write a recursive polymorphic function using Shapeless

I can write a simple recursive polymorphic function:

object simpleRec extends Poly1 { implicit def caseInt = at[Int](identity) implicit def caseList[A, B](implicit ev: simpleRec.Case.Aux[A, B]) = at[List[A]](_.headOption.map(simpleRec)) } 

This seems to basically do what I want; however, it seems I am getting a meaningless result:

 scala> simpleRec(List.empty[List[Int]]) res3: Option[B] = None scala> simpleRec(List(List(1))) res4: Option[B] = Some(Some(1)) 

How can I get this to give the values ​​of Option[Option[Int]] rather than Option[B] ? I expect that I will make a stupid mistake here, but I can’t understand what it is.

+6
source share
1 answer

It was probably an error, the following code works as expected:

 object simpleRec extends Poly1 { implicit def caseInt = at[Int](identity) implicit def caseList[A](implicit ev: simpleRec.Case[A]) = at[List[A]](_.headOption.map(simpleRec)) } 

Using shapeless_2.11-2.0.0

+2
source

All Articles