shapeless

Shapeless mapping and subtype polymorphism with custom type bound

六月ゝ 毕业季﹏ 提交于 2019-12-07 20:48:23
问题 Trying to map an HList of a custom polymorphic class I'm getting the dreaded "could not find implicit value for parameter mapper" error. A code sample: import shapeless._ trait SubTrait case class A() extends SubTrait case class B() extends SubTrait case class C[T <: SubTrait](x: T) object TheMapper extends Poly1 { implicit def default[T <: SubTrait, L[T] <: C[T]] = at[L[T]](_.x) } val ab = C(A()) :: C(B()) :: HNil println(ab.map(TheMapper)) This works fine if the bound for L[T] is e.g.

Is there infrastructure in shapeless that takes a type constructor to the power of a Nat?

五迷三道 提交于 2019-12-07 18:40:06
问题 To me this appears as a very basic functionality, but I can't find it in current shapeless (2.3.3). So I am looking for a type Induction[X,F[_],N <: Nat] with Induction[X,F,Nat._0].Out =:= X Induction[X,F,Nat._1].Out =:= F[X] Induction[X,F,Nat._2].Out =:= F[F[X]] ... Maybe it's also possible to chain a function along the type construction, e.g., to construct a Point instance? 回答1: No there isn't. As you observe this most likely needs a Point -like type class to be useful. I suggest adding

Invoke a Scala Function2 with a shapeless HList whose values do not match the argument order

倖福魔咒の 提交于 2019-12-07 17:47:43
问题 I'd like to build the equivalent of: def applyWithHList2[A1, A2, R, L <: HList](l: L, f: Function2[A1, A2, R]): Try[R] The values in the list are such that in the N choose 2 possible value combinations of l.unify there is at most one that could be used to call the function. No additional type information is available. If there is no way to call the function, the result should be Failure with MatchError . Otherwise, the result should be Try(f(a1, a2)) . I am still getting used to shapeless and

Tuple to HList using productElements

不想你离开。 提交于 2019-12-07 14:49:53
问题 I am using Shapeless 2.2.5. I try to convert a tuple to HList using the code below. import shapeless._ import syntax.std.product._ (23, "foo", 2.0, true).productElements But I get a compilation error. [error] /scala/testScala/src/test/scala/lombok/shapeless/TestTuple2HList.scala:12: could not find implicit value for parameter gen: shapeless.Generic[(Int, String, Double, Boolean)] [error] (23, "foo", 2.0, true).productElements The test conversions.scala in https://github.com/milessabin

Compiler cannot find right implicits for shapeless LabelledGeneric

ぐ巨炮叔叔 提交于 2019-12-07 14:11:47
问题 Continuing the discussion about converting case classes and Map[String,Any] , I faced some problems when I wanted to use it in a generic way, Here is the main discussion. And when I want to use a generic method like following, compiler complains about the implicits: import MapConvertor._ def convert[A](cc: A)= { cc.toMapRec } Here is the whole code that provides toMapRec : import shapeless._, labelled.FieldType, record._ trait ToMapRec[L <: HList] { def apply(l: L): Map[String, Any] } trait

Passing an extra argument into a polymorphic function?

断了今生、忘了曾经 提交于 2019-12-07 13:25:40
问题 I have a polymorphic function which can turn lists into sets: import shapeless.PolyDefns.~> import shapeless._ val lists = List(1,2) :: List("A", "B") :: List(1.1, 2.2) :: HNil object sss extends (List ~> Set) { def apply[T](l:List[T]):Set[T] = { l.toSet } } lists.map(sss) // I want: Set(1,2) :: Set("A", "B") :: Set(1.1, 2.2) :: HNil But what if I want to change the behavior of this function - I now want to add an extra argument which will specify which item in the input list should be put

How to extract an element from an HList with a specific (parameterized) type

最后都变了- 提交于 2019-12-07 12:55:30
问题 I'm chaining transformations, and I'd like to accumulate the result of each transformation so that it can potentially be used in any subsequent step, and also so that all the results are available at the end (mostly for debugging purposes). There are several steps and from time to time I need to add a new step or change the inputs for a step. HList seems to offer a convenient way to collect the results in a flexible but still type-safe way. But I'd rather not complicate the actual steps by

Composing typeclasses for tuples in Scala

試著忘記壹切 提交于 2019-12-07 12:25:34
问题 I am looking for abstraction to compose typeclasses and avoid boilerplate code: sealed trait MyTypeClass[T]{ def add(t:T, mystuff:Something) } object MyTypeClass { implicit def tupled[A,B](implicit adder1: MyTypeClass [A],adder2: MyTypeClass [B]): MyTypeClass [(A,B)] = new MyTypeClass [(A, B)] { override def add(t: (A, B), mystuff: Something): Unit = { val (a,b) = t adder1 add a adder2 add b } } } Is there a boilerplate free approach ? Maybe in shapeless ? 回答1: Yep, Shapeless can help you

Clean up signatures with long implicit parameter lists

天大地大妈咪最大 提交于 2019-12-07 12:14:40
问题 Is there an elegant solution to somehow clean up implicit parameter lists making signatures more concise? I have code like this: import shapeless._ import shapeless.HList._ import shapeless.ops.hlist._ import shapeless.poly._ trait T[I, O] extends (I => O) trait Validator[P] object Validator{ def apply[P] = new Validator[P]{} } object valid extends Poly1 { implicit def caseFunction[In, Out] = at[T[In, Out]](f => Validator[In]) } object isValid extends Poly2 { implicit def caseFolder[Last, New

Dependent type seems to “not work” when generated by Scala macro

眉间皱痕 提交于 2019-12-07 11:55:36
问题 Apologies for the handwavey title. I’m not entirely sure how to phrase the question succinctly, since I’ve never encountered something like this before. Background info: I have the following trait, where the type U is meant to hold a Shapeless extensible record type: trait Flattened[T] { type U <: shapeless.HList def fields: U } I’m using a blackbox macro (for reasons outside the scope of this question) to create new instances of the trait: object flatten { import scala.language.experimental