shapeless

Using shapeless HLists with invariant containers

99封情书 提交于 2019-12-23 02:02:21
问题 Suppose the elements of an HList are subclasses of a generic trait. Each element is contained in case class Box[E](elem E) . That Box is invariant in E causes problems with mapping a poly1 over HList , selecting an element by its parent trait, etc. Here's an example: import shapeless._ trait Drink[+A]{ def v: A} case class Water(v: Int) extends Drink[Int] case class Juice(v: BigDecimal) extends Drink[BigDecimal] case class Squash(v: BigDecimal) extends Drink[BigDecimal] case class Box[E](elem

Using shapeless HLists with invariant containers

好久不见. 提交于 2019-12-23 02:02:06
问题 Suppose the elements of an HList are subclasses of a generic trait. Each element is contained in case class Box[E](elem E) . That Box is invariant in E causes problems with mapping a poly1 over HList , selecting an element by its parent trait, etc. Here's an example: import shapeless._ trait Drink[+A]{ def v: A} case class Water(v: Int) extends Drink[Int] case class Juice(v: BigDecimal) extends Drink[BigDecimal] case class Squash(v: BigDecimal) extends Drink[BigDecimal] case class Box[E](elem

Shapeless not working in generic context

社会主义新天地 提交于 2019-12-22 09:49:05
问题 I am still trying to get my head around Shapeless (and, to a lesser extent, Scala!) and I have been writing some simple code to generate random instance data for case classes - predominantly based on the guides here: http://enear.github.io/2016/09/27/bits-of-shapeless-2/ (the example covers a JSON Writer implementation) I have created a Generator[A] trait and created implicit implementations for simple types, and as per the example in the above link, I have also created implicit

Polymorphic functions with constant return type in Shapeless

北慕城南 提交于 2019-12-22 09:24:23
问题 Long story short, I'm trying to figure out how to define a function from a generic input to a single-typed output. The background: This is a continuation of Mapping over Shapeless record. After Travis's excellent answer, I now have the following: import shapeless._ import poly._ import syntax.singleton._ import record._ type QueryParams = Map[String, Seq[String]] trait RequestParam[T] { def value: T /** Convert value back to a query parameter representation */ def toQueryParams: Seq[(String,

Type inference on contents of shapeless HList

冷暖自知 提交于 2019-12-22 08:45:30
问题 This example is simplified. I have a set of classes like this: case class KeyMapping[KeyType](k:KeyType) class WrappedMapping[KeyType](m:T forSome {type T <: KeyMapping[KeyType]}) { val k:KeyType = ??? } In the following code the types are correctly inferred: val w = new WrappedMapping(KeyMapping("key")) //The statement below gives the correct error //type mismatch; // found : w.k.type (with underlying type String) required: Nothing //val test1:Nothing = w.k I have no clue how to infer the

how to use shapeless to detect field type annotation

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 06:49:40
问题 I am trying to gather the fields of a case class that have a particular annotations at compile time using shapeless . I tried to play around the following snippet, but it did not work as expected (output nothing instead of printing "i"). How can I make it work ? import shapeless._ import shapeless.labelled._ final class searchable() extends scala.annotation.StaticAnnotation final case class Foo(@searchable i: Int, s: String) trait Boo[A] { def print(a: A): Unit } sealed trait Boo0 { implicit

The length of HList type paremeter in terms of Nat

会有一股神秘感。 提交于 2019-12-21 21:31:07
问题 Suppose I have a method without params. How can I determine a length of type parameter? def func[T <: HList]: Nat = { // some magic } 回答1: You can use ops.hlist.Length operation to calculate the Nat length of an HList . Also, getting it as an opaque Nat is not very useful, because you lose all the type-level information about the actual number. So you have to get the exact Nat type from the function: import shapeless._ import shapeless.ops.hlist.Length def func[T <: HList](implicit len:

Understanding `Monomorphism` Example of Shapeless

孤者浪人 提交于 2019-12-21 12:57:55
问题 The Shapeless Features Overview shows the following example: import poly._ // choose is a function from Sets to Options with no type specific cases object choose extends (Set ~> Option) { def apply[T](s : Set[T]) = s.headOption } scala> choose(Set(1, 2, 3)) res0: Option[Int] = Some(1) scala> choose(Set('a', 'b', 'c')) res1: Option[Char] = Some(a) However, I don't, in m lack of experience with Shapeless, understand the difference between that and the following: scala> def f[T](set: Set[T]):

what is Case.Aux in shapeless

一世执手 提交于 2019-12-21 09:13:09
问题 I am confused about the example shown in the shapeless feature overview. object size extends Poly1 { implicit def caseInt = at[Int](x => 1) implicit def caseString = at[String](_.length) implicit def caseTuple[T, U] (implicit st : Case.Aux[T, Int], su : Case.Aux[U, Int]) = at[(T, U)](t => size(t._1)+size(t._2)) } scala> size(((23, "foo"), 13)) res7: Int = 5 what is the Case.Aux ? why is parameterised type is Int not String if size(((23, "foo", 123), 13)), how to define CaseTuple ? Many thanks

Convert scala List[String]/List[Object] into model/HList/tuple

好久不见. 提交于 2019-12-21 05:43:08
问题 An external system returns Seq[String] (kind of DB, output like CSV/json), it's wrap of base types: string/numbers. I'd rather work with my own model. object Converter { type Output = (Int, String, Double) // for instance def convert(values: List[String]): Output } Obviously, I do not want to implement convert-method every time. Seems like I need something simpler than http://nrinaudo.github.io/tabulate/tut/parsing.html Is it possible to use HList here? Like conversion sized HList (String::