Assuming if I have the following Record typed data, and a hlist of keys:
val rr = ("a" ->> 1) ::
("b" -> "s") ::
("c" -> 3) ::
HNil
val hh = "c" :: "b" :: HNil
And I want to extract values in rr for each key in hh, then combine them into a type level object, eventually yielding:
(3: Int) :: ("s": String) :: HNil
How this can be achieved with least amount of code? I could obviously write a inductively-summoned implicit function but it seems to be overkill
Firstly, you have typos.
->>should be instead of->.Secondly,
val hh = "c" :: "b" :: HNildoesn't have type"c" :: "b" :: HNil, it has typeString :: String :: HNilinstead (so you're loosing compile-time information about keys). If you wanthhto have type"c" :: "b" :: HNil(so that values with such keys can be extracted fromrr) then you should use.narrowThirdly, if you want to extract a value from a record by a key you should use type class
shapeless.ops.record.Selector. If you want to extract several values by several keys you should use type classshapeless.ops.record.SelectAll. There is extension methodget/applydefined viaSelectorinshapeless.record._but I can't find the one defined viaSelectAllso you can define it yourself similarly toget/apply