shapeless

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

白昼怎懂夜的黑 提交于 2019-12-06 02:56:09
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 making them deal with the HList and the accompanying business. Here's a simplified version of the

Adding Container types like Seq, List, Option, and Map to shapeless generic conversion

牧云@^-^@ 提交于 2019-12-06 00:20:22
问题 In order to complete the series of questions that I've asked about conversion of case class to/from Map[String, Any] in 1 and 2, I wanted to know how can one extend those answers mentioned above to work with container types, specially: Option Seq Set Map Tuple s In both case of case class from and to Map[String, Any] 来源: https://stackoverflow.com/questions/31736369/adding-container-types-like-seq-list-option-and-map-to-shapeless-generic-conv

leftReduce Shapeless HList of generic types

拈花ヽ惹草 提交于 2019-12-05 23:33:01
问题 This is essentially what I want: case class Foo[T](x: T) object combine extends Poly { implicit def caseFoo[A, B] = use((f1: Foo[A], f2: Foo[B]) => Foo((f1.x, f2.x))) } def combineHLatest[L <: HList](l: L) = l.reduceLeft(combine) So combineHLatest(Foo(1) :: Foo("hello") :: HNil) should yield Foo( (1, "hello") ) The above doesn't compile as it cannot find an implicit LeftReducer but I'm at a loss as to how to implement one. 回答1: It was pretty late last night when I answered this, and while the

Compiler cannot find right implicits for shapeless LabelledGeneric

蓝咒 提交于 2019-12-05 22:14:51
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 LowPriorityToMapRec { implicit def hconsToMapRec1[K <: Symbol, V, T <: HList](implicit wit: Witness.Aux

Map on HList fails with subtypes of generic type in Scala & Shapeless

[亡魂溺海] 提交于 2019-12-05 21:24:23
问题 Say we have the following classes and some values (in Scala): class A[T](val x: T) class B[T](x: T, val y: T) extends A[T](x) val x1 = new A("test") val x2 = new B(1,2) val x3 = new B("foo","bar") val x4 = new A(1) Further, we define the following polymorphic function value (using shapeless): object f extends (A ~> Option) { def apply[T](s: A[T]) = Some(s.x) } Now we can call: f(x1); f(x2); f(x3); f(x4) Which all succeed (and should IMHO). However: val list = x1 :: x2 :: x3 :: x4 :: HNil list

Polymorphic functions with constant return type in Shapeless

£可爱£侵袭症+ 提交于 2019-12-05 20:15:08
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, String)] /** Mark this parameter for auto-propagation in new URLs */ def propagate: Boolean protected

Map a homogenous HList of one type into a hetergenous HList of different types

送分小仙女□ 提交于 2019-12-05 18:28:00
I have an HList of strings: val strings = "The Lorax" :: "Dr. Suess" :: HNil I another HList of special types: case class Title(title: String, words: List[String]) case class Author(firstName: String, lastName: String) val book = Title("The Hobbit", List("Hobbit")) :: Author("J.R.R.", "Tolkien") :: HNil I want to turn "strings", my HList of strings, into an HList of mixed types, corresponding to the "book" list. If I have a method to go from a string -> Title, and a method to go from a string -> Author, I feel like this should be very straight forward to essentially get "strings" as an

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

╄→尐↘猪︶ㄣ 提交于 2019-12-05 18:27:17
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.macros import scala.reflect.macros.blackbox.Context def apply[T](struct: T): Flattened[T] = macro impl

Update case class from incomplete JSON with Argonaut or Circe

这一生的挚爱 提交于 2019-12-05 17:43:10
问题 I need to create an updated instance from a case class instance (with any needed DecodeJson s implicitly derived), given an incomplete json (some fields missing). How can this be accomplished with Argonaut (preferably) or Circe (if I have to)? Example: case class Person(name:String, age:Int) val person = Person("mr complete", 42) val incompletePersonJson = """{"name":"mr updated"}""" val updatedPerson = updateCaseClassFromIncompleteJson(person, incompletePersonJson) println(updatedPerson) /

Cannot get type of generic object in a list

时间秒杀一切 提交于 2019-12-05 16:08:04
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 type C (or subtype) in several storages, each of them containing elements of a single type. I am