shapeless

Extract FieldType key and value from HList

ぐ巨炮叔叔 提交于 2021-02-05 08:12:56
问题 I would like to extract the key and value of the head of an HList using these two methods: def getFieldName[K, V](value: FieldType[K, V])(implicit witness: Witness.Aux[K]): K = witness.value def getFieldValue[K, V](value: FieldType[K, V]): V = value I tried a few variations of this function, but I couldn't make it work, I think this might be the closest to the right solution: def getFieldNameValue[Key <: Symbol, Value <: AnyRef, Y <: HList, A <: Product](a : A) (implicit gen : LabelledGeneric

How to infer inner type of Shapeless record value with unary type constructor?

孤人 提交于 2021-02-04 12:36:52
问题 I'm having trouble understanding the way the Shapeless record Selector interacts with scala's type inference. I'm trying to create a method that can grab a field from a Shapeless record by key, only if the value of the field has a certain unary type constructor, in this particular case Vector[_] , and then grab an inner value of inferred type V out of the Vector , in this case with Vector.apply() . I feel like I'm close. This works, with a concrete inner type of Int : val record = ( "a" ->>

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

Shapeless and annotations

99封情书 提交于 2021-01-29 07:23:18
问题 I would like to have some function applied to fields in a case class, that are annotated with MyAnnotation . The idea is to transform type T into its generic representation, extract annotations, zip, fold right (or left) to reconstruct a generic representation and finally get back to type T . I followed the answer provided here and this gist. I'm using scala 2.11.12 and shapeless 2.3.3. Hereafter is my code: import shapeless._ import shapeless.ops.hlist._ case class MyAnnotation(func: String)

When using HList with GADTs I am having to cast using asInstanceOf[H]. Is there a way to avoid the cast?

孤者浪人 提交于 2021-01-27 12:24:51
问题 Given 2 GADT Algebras which know about each other and 2 interpreters that are mutually recursive, I am having issues having to cast from type A to type h <: HList even though in the context of the pattern match, it should be implied that type A is type h. Is there a way to avoid the asInstanceOf[h] call in the interpreter? abstract class KvpHList[H<:HList] object KvpNil extends KvpHList[HNil] case class KvpCons[H <: A :: T,A, T<:HList](head: KvpValue[A], tail: KvpHList[T])(implicit isHCons:

Reverse HList and convert to class?

喜你入骨 提交于 2021-01-27 11:33:12
问题 I'm using Shapeless to accumulate materialized values in Akka as an HList and convert that to a case class. (You don't have to know Akka much for this question, but the default approach accumulates materialized values as recursively nested 2-tuples, which isn't much fun, so Shapeless HLists seemed a more sensible approach -- and works pretty well. But I don't know how to properly re-use that approach. Here, I'll simplify the kinds of values Akka produces.) For example, let's say we've got two

Explain the `LowPriorityImplicits` pattern used in Scala type-level programming

情到浓时终转凉″ 提交于 2021-01-27 01:50:12
问题 When looking at the source of some Scala libraries, e.g. shapeless, I often find traits named LowPriorityImplicits . Can you please explain this pattern? What is the problem that is solved, and how does the pattern solve it? 回答1: That pattern allows you to have hierarchy of implicits avoiding ambiguity-related errors by the compiler and providing a way to prioritise them. As an example consider the following: trait MyTypeclass[T] { def foo: String } object MyTypeclass { implicit def

mapping over HList inside a function

我的梦境 提交于 2020-12-29 06:35:31
问题 The following code seems obvious enough to compile and run case class Pair(a: String, b: Int) val pairGen = Generic[Pair] object size extends Poly1 { implicit def caseInt = at[Int](x => 1) implicit def caseString = at[String](_.length) } def funrun(p: Pair) = { val hp: HList = pairGen.to(p) hp.map(size) } but the compiler says "could not find implicit value for parameter mapper". In my use case, I want to map over an HList to get and HList of String(s) and then convert the HList of String(s)

mapping over HList inside a function

瘦欲@ 提交于 2020-12-29 06:35:20
问题 The following code seems obvious enough to compile and run case class Pair(a: String, b: Int) val pairGen = Generic[Pair] object size extends Poly1 { implicit def caseInt = at[Int](x => 1) implicit def caseString = at[String](_.length) } def funrun(p: Pair) = { val hp: HList = pairGen.to(p) hp.map(size) } but the compiler says "could not find implicit value for parameter mapper". In my use case, I want to map over an HList to get and HList of String(s) and then convert the HList of String(s)

Is there a type-class that checks for existence of at least one implicit of a type?

有些话、适合烂在心里 提交于 2020-12-05 11:48:25
问题 I have a trait Foo[T, U] and a type-level algorithm that given an L <: HList and a target type U , tells me whether there exists T in L such that there is an implicit Foo[T, U] in scope. This is implemented using the following type class: trait Search[L <: HList, U] object Search { def apply[L <: HList, U](implicit s: Search[L, U]): U = null ... } and we have the following: object Test { type L = Int :: String :: HNil implicit val foo: Foo[String, Boolean] = null Search[L, Boolean] //compiles