lets say I have the following class hierarchy:
sealed trait Animal
case class Cat(isFriendly: Boolean) extends Animal
case class Dog(color: String) extends Animal
case class Fish(isFreshWater: Boolean) extends Animal
Now I have an instance of type
Option[Cat] :: Option[Dog] :: Option[Fish] :: HNil
But there is a restriction on the instance. It can only be of one of the following forms
Some(Cat(???)) :: None :: None :: HNil
or
None :: Some(Dog(???)) :: None :: HNil
or
None :: None :: Some(Fish(???)) :: HNil
First, excuse any incoherence - it is part of a larger problem that I am trying to solve that is not yet well articulated
Second, the ???
is just my contrived place holder for real instance such as:
None :: Some(Dog(brown)) :: None :: HNil
Thing is, I am rather new to shapeless and I don't exactly know if the value of the ???
makes a difference.
Onwards to the question
Is there a way to "iterate" over the HList and extract the Some
?
I understand that when speaking generically it is not possible as shown in the following two questions. But I wonder whether adding the restrictions I set above would make a difference
https://mcmap.net/q/1779306/-generic-poly2-folder-case-for-shapeless-hlist
https://mcmap.net/q/1779307/-shapeless-flatmap-hlist-with-option-yielding-hlist
HList
, but rather aCoproduct
, since that is exactly what you want: a list of possible types only one of which has a value. – TonneHLists
... Anyway, is there a way to convert theHList
to aCoproduct
? Also, while I try to work it out myself, if you might point me in the right direction it would be highly appreciated :) – Sisson(None :: Some(1) :: HNil).toList.find(_.isDefined)
. But as a co-product would be better. – Ernaldus