1

So, there are hlists of options:

val l1 = Option(1) :: Option("str") :: Option(true) :: HNil
val l2 = Option(1) :: {None: Option[String]} :: Option(false) :: HNil

To make a new list, consisting of all matching type objects (of their TypeTags), it is possible using the liftA2 function ( https://stackoverflow.com/a/14456938/1092399 )(notice: there are repetitive types and lots of HNil objects in a result, but it is not a question):

object eq extends Poly2 {
    implicit def neq[A, B](implicit e: A =:!= B) = 
        at[Option[A], Option[B]] { case (a, b) => (HNil) }
    implicit def eq[A, B](implicit t: TypeTag[A], e: A =:= B) =
        at[Option[A], Option[B]] {(a, b) => t :: HNil}
}

liftA2(eq)(l1, l2) //> there would be all three types

So now the objective is to skip all empty Option objects (if we can do this, so there is a chance to build a list of matching all empty Option objects); we need smth like:

object eqo extends Poly2 {
    implicit def neq[A, B](implicit e: A =:!= B) = 
        at[Option[A], Option[B]] { case (a, b) => (HNil) }
    implicit def eq[A, B](implicit t: TypeTag[A], e: A =:= B) = 
        at[Option[A], Option[B]] {
            case(a, None)  => HNil
            case (None, b) => HNil
            case (a, b)    => t :: HNil
        }
}

liftA2(eqo)(l1, l2) //> result with no String type (should be)

The function above is wrong, cause there happens type erasure (types in a result ~ Product with Serializable with shapeless.HList). So the question: is there a way to make such check or it is completely unreal?

p.s. It is a very interesting type of case in this situation: Product with Serializable with shapeless.HList

Community
  • 1
  • 1
DaunnC
  • 1,301
  • 15
  • 30
  • 1
    Erasure isn't the problem—the return type of `eqo.eq` really is just `HList` (you can basically ignore the `Product with Serializable` part). You want a type that depends on values, and I can't see a straightforward way to encode that here. – Travis Brown Mar 23 '14 at 21:37
  • Thx for the answer, heh, then I'll try to make it with a type class, and try to remove empty options from the list before usage `liftA2`. – DaunnC Mar 24 '14 at 05:59

0 Answers0