shapeless

How to define a function whose output type depends on the input type

夙愿已清 提交于 2019-12-02 19:11:35
Given the following classes: case class AddRequest(x: Int, y: Int) case class AddResponse(sum: Int) case class ToUppercaseRequest(str: String) case class ToUppercaseResponse(upper: String) How do I define in a typesafe manner some function: def process(req: ???): ??? Such that the following should hold true: val r1: AddResponse = process(AddRequest(2, 3)) val r2: ToUppercaseResponse = process(ToUppercaseRequest("aaa")) Also, the following should not compile: val r3 = process("somestring") This is both entirely possible and a totally reasonable thing to do in Scala. This kind of thing is all

Can Map be performed on a Scala HList

寵の児 提交于 2019-12-02 15:50:26
I have done a few implementations of HList now. One based on Daniel Spiewak's High Wizardry in the Land of Scala talk and another based on a post in Apocalisp blog. The goal was to have a heterogenous list of which is not heterogenous in the primary type but rather the higher kind. For example: val requests = Request[String] :: Request[Int] :: HNil I would be able to do a map across the list to perform the request and result in a heterogenous list of the higher kind. So: requests.map(execute) should equal String :: Int :: HNil Sadly all my attempts have resulted in an HList of Any. Here is the

Implicit arguments: how to encode in function signature?

妖精的绣舞 提交于 2019-12-02 15:21:44
问题 Following the awesomely enlightening question by @TravisBrown concerning the enumeration of ADTs using shapeless, I am left with the following code snippet: implicitly[EnumerableAdt[Foo]].values I would like to encapsulate this within a method so that I don't have to .values after each invocation (It seems a cleaner API to me, that way). But I can't seem to get it right. Whenever I try to encapsulate the implicitly[EnumerableAdt[Foo]] I get implicit resolution errors. What I had tried, that

Shapeless: Generic.Aux

两盒软妹~` 提交于 2019-12-02 14:18:51
I'm trying to understand how Generic works (and TypeClass too). The github wiki is very sparse on examples and documentation. Is there a canonical blog post / documentation page describing Generic and TypeClass in detail? In concrete, what is the difference between these two methods?: def find1[T](implicit gen: Generic[T]): Generic[T] = gen def find2[T](implicit gen: Generic[T]): Generic[T] { type Repr = gen.Repr } = gen given object Generic { type Aux[T, Repr0] = Generic[T] { type Repr = Repr0 } def apply[T](implicit gen: Generic[T]): Aux[T, gen.Repr] = gen implicit def materialize[T, R]: Aux

Enforce Bounded Nat?

点点圈 提交于 2019-12-02 12:32:40
How can I enforce a Nat that's <= N ? Example: def lessThan5(x: NatLT5) = ??? where lessThan5(Nat(4)) would compile, but lessThan5(Nat(6)) would not. You can use the type class LTEq (or LT if you want strictly less than). import shapeless.nat._ import shapeless.ops.nat._ def lessThan5[N <: Nat](n: N)(implicit ev: LTEq[N, _5]) = ??? lessThan5(_4) // compiles lessThan5(_5) // compiles lessThan5(_6) // doesn't compile because LTEq[_6, _5] cannot be found 来源: https://stackoverflow.com/questions/39156628/enforce-bounded-nat

How to use shapeless to convert generic Map[String, Any] to case class inside generic function?

核能气质少年 提交于 2019-12-02 06:12:37
问题 I'm trying to follow the answer here https://stackoverflow.com/a/31641779/1586965 That is, I want to be able to convert (potentially nested) Map[String, Any] to a case class. scalaVersion := "2.11.8" val shapelessV = "2.3.3" If I try to wrap the code in the above answer in another generic function, I can't seem to get it to compile import shapeless._, labelled._ import FromMap._ def usesGenerics[P](map: Map[String, Any]): P = { to[P].from(mp).get } I get the following compile error could not

Shapeless: own HList constraint using Coproduct

假如想象 提交于 2019-12-01 23:08:50
(NOTE: Split from Shapeless: Trying to restrict HList elements by their type ) Question 2 - Own Constraint using Coproduct What I really wanted to do is to write a new constraint using Coproduct. trait CPConstraint[L <: HList, CP <: Coproduct] extends Serializable object CPConstraint { import shapeless.ops.coproduct.Selector._ def apply[L <: HList, CP <: Coproduct](implicit cpc: CPConstraint[L, CP]): CPConstraint[L, CP] = cpc type <*<[CP <: Coproduct] = { // TODO: just invented a symbol ... what would be an appropriate one? type λ[L <: HList] = CPConstraint[L, CP] } implicit def hnilCP[HN <:

What does the Aux pattern accomplish in Scala?

旧城冷巷雨未停 提交于 2019-12-01 18:18:10
I have a bit of a sense of the Aux pattern (as used in shapeless and elsewhere) in which a type member is extracted into a type parameter, and I know it's a workaround the fact that arguments in the same argument list can't depend on each other -- but I'm not clear generally what it's used for and what problems it solves. For example, I'm currently trying to figure out how to preserve and work with the more specific type returned by a whitebox macro -- is this a usecase for Aux? Is there a simple description? Simply speaking this pattern lets you establish a relation between two generic type

What does the Aux pattern accomplish in Scala?

江枫思渺然 提交于 2019-12-01 17:12:51
问题 I have a bit of a sense of the Aux pattern (as used in shapeless and elsewhere) in which a type member is extracted into a type parameter, and I know it's a workaround the fact that arguments in the same argument list can't depend on each other -- but I'm not clear generally what it's used for and what problems it solves. For example, I'm currently trying to figure out how to preserve and work with the more specific type returned by a whitebox macro -- is this a usecase for Aux? Is there a

Tuple to HList using productElements

≯℡__Kan透↙ 提交于 2019-12-01 08:33:54
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/shapeless/blob/master/core/src/test/scala/shapeless/conversions.scala did not provide an implicit value for