Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

scala - flatMap with Shapeless yield FlatMapper not found

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))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...