问题
I'm trying to define some structure like this
case class Transformer[From, To](
name: String,
get: PaymentEvent => From,
to: From => To
I want to filter elements with names that are part of a Set
class filterName(names: Set[String]) extends lowPriority {
implicit def get[From, To] = at[Transformer[From, To]]{ trans =>
if (names.contains(trans.name))
trans :: HNil
else
HNil
}
}
This is the concrete value:
type HTransformer = Transformer[String, String] :: Transformer[Long, Long] :: HNil
When I want to apply the function to the value
private def filter(fields: HTransformer, names: Set[String]): HTransformer = {
fields.flatMap(new filterName(names))
}
The compiler reports errors:
Error:(42, 19) could not find implicit value for parameter mapper: shapeless.ops.hlist.FlatMapper[f.type,shapeless.::[com.ingenico.datalake.Transformer[String,String],shapeless.::[com.ingenico.datalake.Transformer[Long,Long],shapeless.HNil]]]
fields.flatMap(new filterName(names))
回答1:
I guess you misunderstand type-level calculations.
If you want to filter an hlist depending on whether an element is a part of the Set then you must know this (if an element is a part of the Set) at compile time but actually you know this only at runtime. So filterName
will not work.
For example you can transform the hlist to a list and filter it as an ordinary collection at runtime.
来源:https://stackoverflow.com/questions/55708142/flatmap-with-shapeless-yield-flatmapper-not-found