shapeless

Cannot get type of generic object in a list

雨燕双飞 提交于 2019-12-07 08:17:12
问题 I have the following trait: trait Storage[C <: Config] { def get(name: String, version: Int): Option[C] def list: List[(String, String)] def register(config: C): Boolean } and I want to create the following class: class MultiStorage[C <: Config](storages: List[Storage[_ <: C]]) extends Storage[C] { def get(name: String, version: Int): Option[C] = {...} def list: List[(String, String)] = {...} def register(config: C) = {...} If not clear, the meaning is that a MultiStorage stores elements of

Converting case class to another recursively structural identical case class

☆樱花仙子☆ 提交于 2019-12-07 07:54:56
问题 I am trying to use Shapeless to convert case class like these: case class A(int: Int, str: String) case class B(a: A, str: String) case class AnotherA(int: Int, str: String) case class AnotherB(a: AnotherA, str: String) Using Generic I can easily convert between A and AnotherA , but not for B and AnotherB because (of course) the a member is of different type. I think I can convert case class into a nested HList following this example, but how do I convert the nested HList back to case class

How can I customize Scala ambiguous implicit errors when using shapeless type inequalities

让人想犯罪 __ 提交于 2019-12-07 07:47:59
问题 def typeSafeSum[T <: Nat, W <: Nat, R <: Nat](x: T, y: W) (implicit sum: Sum.Aux[T, W, R], error: R =:!= _7) = x typeSafeSum(_3, _4) //compilation error, ambiguous implicit found. I dont think that error message "ambiguous implicit found" is friendly, how can I customize it to say something like "the sum of 2 NAT value should not equal to 7" Many thanks in advance 回答1: shapeless's =:!= (and similar type inequality operators) inherently exploit ambiguous implicits to encode Prolog-style

Do a covariant filter on an HList

陌路散爱 提交于 2019-12-07 06:27:29
问题 I intend to filter on an HList in a covariant manner - I would like to include subclasses as well. So the covariant filter on Foo should capture elements of Foo as well as Bar . I've constructed this example trying out <:!< , to see if it does what I would like it to do. http://scastie.org/6465 /*** scalaVersion := "2.11.2" libraryDependencies ++= Seq( "com.chuusai" %% "shapeless" % "2.0.0" ) */ import shapeless._ final class HListOps[L <: HList](l: L) { trait CoFilter[L <: HList, U] extends

Generic Poly2 Folder case for shapeless Hlist

こ雲淡風輕ζ 提交于 2019-12-07 05:29:51
问题 I am trying to transform the following HList Some(C(15)) :: None :: Some(B(55)) :: None :: Some(A(195)) :: HNil to C(15) :: B(55) :: A(195) :: HNil Here is what I have at the moment : import shapeless._ case class A(value: Int) case class B(value: Int) case class C(value: Int) trait folderLP extends Poly2 { implicit def default[T, L <: HList] = at[T, L]((acc, t) => acc) } object folder extends folderLP { implicit def none[T, L <: HList] = at[None.type, L]((t, acc) => acc) implicit def

Why does mapping over an HList of Option[T] not work?

三世轮回 提交于 2019-12-07 01:56:27
问题 This does not compile and I do not understand why: import shapeless._ import poly._ object option extends (Option ~> List) { def apply[T](t: Option[T]) = t.toList } val simple = Some(1) :: Some("hello") :: Some(true) :: HNil val opts: List[Int] :: List[String] :: List[Boolean] :: HNil = simple map option This is somewhat surprising, because this does compile: import shapeless._ import poly._ object choose extends (Set ~> Option) { def apply[T](s: Set[T]) = s.headOption } val sets = Set(1) ::

Deriving decoder instances of case classes with a single field

青春壹個敷衍的年華 提交于 2019-12-06 16:49:30
This question is tightly related to this one , but not identical: it's the same context but the opposite problem. I'm trying to derive a CellDecoder[A] , which is essentially a String => A , for A s that are case classes with a single field whose type has a CellDecoder . In order to do this, I need to require: A to be transformable to R , where R is a subtype of H :: HNil . H to have CellDecoder instance. That is: implicit def caseClassCellDecoder[A, R, H](implicit gen: Generic.Aux[A, R], ev: R <:< (H :: HNil), d: CellDecoder[H] ): CellDecoder[A] = ??? The issue I'm having is that, once I've

Provide implicits for all subtypes of sealed type

自闭症网瘾萝莉.ら 提交于 2019-12-06 14:58:58
In my application I have multiple case classes and objects which are part of sealed trait hierarchy. I use them as messages in Akka. Those classes need to be converted to user friendly form before sending through websocket. Previously I used big pattern match to convert them in single place, but as number of types grows I would like to use implicit conversion: object Types { sealed trait Type case object SubType1 extends Type case object SubType2 extends Type case object SubType3 extends Type trait Converter[T] { def convert(t: T): Int } } object Implicits { import Types._ implicit object

Parse List[String] into HList

北城余情 提交于 2019-12-06 13:57:34
问题 I would like to write def parse[T <: HList](list: List[String]): Validation[T] . list could be List("fooid", "barid") , and T FooId :: BarId :: HNil , and a typeclass Parse[T] which implements String => Validation[FooId] . How would I write said parse , which parses the list into T ? I'm not sure how to summon the implicit typeclasses for each of the elements of T . 回答1: We can adapt the code from shapeless.ops.traversable.FromTraversable. I'm not sure what your Validation type is, but I used

Shapeless mapping and subtype polymorphism with custom type bound

試著忘記壹切 提交于 2019-12-06 13:13:40
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. Iterable (see this very similar question , solution, and comments). What am I missing? For some reason the