type-level-computation

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

Scala compiler expand types

时光毁灭记忆、已成空白 提交于 2021-01-29 12:23:07
问题 Consider this code: trait TypeOr[E, F] { type T } implicit def noneq2[E, F](implicit ev: E =!= F): TypeOr[E, F] = new TypeOr[E, F] { type T = (E, F) } sealed trait Error[+E, +A] case class Err[E, A](e: Error[E, A]) { def combine[B, F](f: A => Error[F, B])(implicit ev: TypeOr[E, F]): Error[ev.T, B] = ??? } val result = Err(null.asInstanceOf[Error[Int, Int]]).combine(_ => null.asInstanceOf[Error[String, String]]) So far so good. From the definitions above, I concluded, that the expanded type of

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

How to create an instances for typeclass with dependent type using shapeless

旧城冷巷雨未停 提交于 2020-11-29 09:32:58
问题 I'm trying to derive a tuple instance for a type class with dependent type. I'm using shapeless to create summon the type class for the tuple elements. I'm having trouble matching tuple instance types: import shapeless.the import simulacrum.typeclass @typeclass trait Identifiable[M] { type K def identify(id: M): K } object Identifiable{ implicit def identifiableTuple[K1: Identifiable, K2: Identifiable]: Identifiable[(K1,K2)] = new Identifiable[(K1,K2)]{ val b = the[Identifiable[K2]] val a =

Why is this implicit ambiguity behaviour happening?

旧城冷巷雨未停 提交于 2020-01-30 05:46:09
问题 I have a typeclass Search , which has an instance Search[A] if we have a TypeClass1[A] or a TypeClass2[A] instance. With preference given to the 1 instance. The following compiles: trait TypeClass1[A] trait TypeClass2[A] trait Search[A] object Search extends LPSearch { implicit def case1[A](implicit ev: TypeClass1[A]): Search[A] = null } trait LPSearch { implicit def case2[A](implicit ev: TypeClass2[A]): Search[A] = null } object Test { implicit val ev1: TypeClass1[Int] = null implicit val

Why is this implicit ambiguity behaviour happening?

落花浮王杯 提交于 2020-01-30 05:46:05
问题 I have a typeclass Search , which has an instance Search[A] if we have a TypeClass1[A] or a TypeClass2[A] instance. With preference given to the 1 instance. The following compiles: trait TypeClass1[A] trait TypeClass2[A] trait Search[A] object Search extends LPSearch { implicit def case1[A](implicit ev: TypeClass1[A]): Search[A] = null } trait LPSearch { implicit def case2[A](implicit ev: TypeClass2[A]): Search[A] = null } object Test { implicit val ev1: TypeClass1[Int] = null implicit val