shapeless

Extract label values from a LabelledGeneric instance

非 Y 不嫁゛ 提交于 2019-12-18 03:55:49
问题 Consider the following example: import shapeless._ case class Foo(bar: String, baz: Boolean) val labl = LabelledGeneric[Foo] Now, the type of labl is (prettified) LabelledGeneric[Foo] { type Repr = FieldType[Symbol @@ String("bar"), String] :: FieldType[Symbol @@ String("baz"), Boolean] :: HNil } which already conveys the information I need, i.e. the names of the case class fields. What I'm looking for is a way to go from labl to something along the lines of "bar" :: "baz" :: HNil i.e.

Shapeless - turn a case class into another with fields in different order

帅比萌擦擦* 提交于 2019-12-17 21:52:27
问题 I'm thinking of doing something similar to Safely copying fields between case classes of different types but with reordered fields, i.e. case class A(foo: Int, bar: Int) case class B(bar: Int, foo: Int) And I'd like to have something to turn a A(3, 4) into a B(4, 3) - shapeless' LabelledGeneric comes to mind, however LabelledGeneric[B].from(LabelledGeneric[A].to(A(12, 13))) results in <console>:15: error: type mismatch; found : shapeless.::[shapeless.record.FieldType[shapeless.tag.@@[Symbol

Passing a Shapeless Extensible Record to a Function (continued)

谁说胖子不能爱 提交于 2019-12-17 18:43:36
问题 Considering this question : Passing a Shapeless Extensible Record to a Function, Travis's answer shows that every function taking an extensible record as parameter must have an implicit selector as parameter. I wonder if one could factorize those declarations in case we have many functions of this kind. E.g. : val w1 = Witness("foo1") val w2 = Witness("foo2") val w3 = Witness("foo3") //Here some "magical" declarations avoiding to declara selectors in fun1, fun2, fun3 below def fun1[L <: HList

Limits of Nat type in Shapeless

时间秒杀一切 提交于 2019-12-17 17:52:50
问题 In shapeless, the Nat type represents a way to encode natural numbers at a type level. This is used for example for fixed size lists. You can even do calculations on type level, e.g. append a list of N elements to a list of K elements and get back a list that is known at compile time to have N+K elements. Is this representation capable of representing large numbers, e.g. 1000000 or 2 53 , or will this cause the Scala compiler to give up? 回答1: I will attempt one myself. I will gladly accept a

Converting Map[String,Any] to a case class using Shapeless

我的梦境 提交于 2019-12-17 10:32:44
问题 The question here asks about mapping a case class to a Map[String,Any]. I was wondering what would be the other way around, converting Map[String,Any] to a case class. Given the following map: val mp = Map("name" -> "Tom", "address" -> Map("street" -> "Jefferson st", "zip" -> 10000)) Convert it to a case class of Person : case class Person(name:String, address:Address) case class Address(street:String, zip:Int) val p = Person("Tom", Address("Jefferson st", 10000)) with something like this:

Can't prove that singleton types are singleton types while generating type class instance

寵の児 提交于 2019-12-17 08:55:22
问题 Suppose I've got a type class that proves that all the types in a Shapeless coproduct are singleton types: import shapeless._ trait AllSingletons[A, C <: Coproduct] { def values: List[A] } object AllSingletons { implicit def cnilSingletons[A]: AllSingletons[A, CNil] = new AllSingletons[A, CNil] { def values = Nil } implicit def coproductSingletons[A, H <: A, T <: Coproduct](implicit tsc: AllSingletons[A, T], witness: Witness.Aux[H] ): AllSingletons[A, H :+: T] = new AllSingletons[A, H :+: T]

Shapeless lenses usage with a string definition

孤街浪徒 提交于 2019-12-13 20:28:16
问题 I would like use shapeless lenses to access value of the case class field by a String definition. I know this code works. case class Test(id: String, calc: Long) val instance = Test("123232", 3434L) val lens = lens[Test] >> 'id val valueOfFieldId = lens.get(instance) But what I am trying to do is: val fieldName = "id" val lens = lens[Test] >> fieldName.witness //I typed .witness because it was expecting a witness (if I am not wrong) val valueOfFieldId = lens.get(instance) But with this code,

Implicit method for typeclass not found

烈酒焚心 提交于 2019-12-13 07:44:56
问题 This is continuation of Diverging implicit expansion for type class. I've came up with another version that still doesn't compile, but now for another reason, hence a different question. Here's what I did: I've basically added a new typeclass NestedParser for the cases where some case class consists of other case classes. trait Parser[A] { def apply(s: String): Option[A] } trait NestedParser[A] { def apply(s: String): Option[A] } object Parser { def apply[A](s: String)(implicit parser: Parser

Is ambiguous implicit value the only way we want to make the error existed in compilation time

与世无争的帅哥 提交于 2019-12-12 08:13:38
问题 trait Foo trait Bar extends Foo def doStuff[T <: Foo](x: T)(implicit ev: T =!:= Foo) = x doStuff(new Foo{}) //ambiguous implicit value doStuff(new Bar)// successful Implicit resolution is happening on compilation time, so in here I think there may be two implicit value with exactly same type to trigger ambiguous stuff. Right now, I am going to introduce shapeless into the team, my colleagues think this ambiguous implicit is not ideal, and I dont have strong argument about it. Is this only way

What does HList#foldLeft() return?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 08:06:56
问题 I'm trying to play with HList's from Shapeless. This is my first try: trait Column[T] { val name: String } case class CV[T](col: Column[T], value: T) object CV { object columnCombinator extends Poly2 { implicit def algo[A] = at[(String, String, String), CV[A]] { case ((suffix, separator, sql), cv) ⇒ (suffix, separator, if (sql == "") cv.col.name+suffix else sql+separator+cv.col.name+suffix) } } def combine[A <: HList](columns: A, suffix: String, separator: String = " and ") (implicit l: