flatMap with Shapeless yield FlatMapper not found

℡╲_俬逩灬. 提交于 2021-02-04 08:10:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!