-
Hello folks, I have following type class hierarchy and use case. There are http request query params, which are represented as trait FilterExtractor[T] {
def extract(input: Seq[(String, String)]): Either[String, T]
} Parsing into case class only possible in a context of key being available, but at some point I need to parse value of query parameter into a type for example trait ValueExtractor[T] {
def extract(input: Seq[String]): Either[String, T]
}
object ValueExtractor {
// givens for simple types
} input here is a sequence, because you could have multiple values per key. object FilterExtractor extends ProductDerivation[FilterExtractor] {
private type StringEither[T] = Either[String, T]
inline def join[DerivationType <: Product : ProductReflection]: FilterExtractor[DerivationType] =
input =>
constructWith[StringEither](
[MonadicTypeIn, MonadicTypeOut] => _.flatMap,
[MonadicType] => Right[String, MonadicType](_),
[FieldType] => context => {
val valuesByKey = input.filter(_._1 == label.s).map(_._2)
???
}
)
} And where implementation is missing now starts my question, how can i produce result of
private given [T: ValueExtractor]: FilterExtractor[T] = input => summon[ValueExtractor[T]].extract(input.map(_._2))
...
context.extract(valuesByKey) both inline and not inline variants throw same error:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
relates to #20, it seems impossible to |
Beta Was this translation helpful? Give feedback.
-
Issue #20 does make it impossible to summon a typeclass parameterized on a polymorphic type parameter, but I hope there's some workaround possible for that. In the meantime, there is one possibility which might work. You could define a combined typeclass:
and then implementing generic derivation on Then, you can provide an implementation of
|
Beta Was this translation helpful? Give feedback.
-
One disadvantage is that this probably comes with some runtime penalty, and also a compiletime penalty if your derivations are complex enough. |
Beta Was this translation helpful? Give feedback.
surprisingly just a simple given conversion started working: