Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write a recursive polymorphic function with 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 largely do what I want; however, I seem to be getting a nonsensical result type:

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

scala> simpleRec(List(List(1)))
res4: Option[B] = Some(Some(1))

How can I make this give me values of Option[Option[Int]] rather than Option[B]? I expect I'm making some silly mistake here, but can't work out what it is.

like image 539
Hugh Avatar asked Jan 22 '14 00:01

Hugh


1 Answers

This probably was a bug, 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

like image 177
EECOLOR Avatar answered Nov 05 '22 10:11

EECOLOR