shapeless

Shapeless: generic lens parameterized by case class or field

可紊 提交于 2019-11-29 04:04:48
Based on: import shapeless._ case class Content(field: Int) lens[Content] >> 'field I am trying to make a lens-creating method, something along: def makeLens[T <: Product](s: Symbol) = lens[T] >> s But it seems non-obvious. Is it possible to do? If not, the end result I'm trying to achieve is a generic method for updating nested Maps with case-class contents, e.g.: import scalaz._ import Scalaz._ import PLens._ import shapeless._ import shapeless.contrib.scalaz._ def nestedMapLens[R, T <: Product](outerKey: String, innerKey: Int, f: Symbol) = ~((lens[T] >> f).asScalaz) compose mapVPLens

Extract label values from a LabelledGeneric instance

∥☆過路亽.° 提交于 2019-11-29 03:11:45
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. materializing the information contained in the singleton types into a value. Is this possible? I could use a

Type casting using type parameter

一世执手 提交于 2019-11-29 01:51:44
Given is a Java method that returns java.lang.Object s for a given string. I'd like to wrap this method in a Scala method that converts the returned instances to some type T . If the conversion fails, the method should return None . I am looking for something similar to this: def convert[T](key: String): Option[T] = { val obj = someJavaMethod(key) // return Some(obj) if obj is of type T, otherwise None } convert[Int]("keyToSomeInt") // yields Some(1) convert[String]("keyToSomeInt") // yields None (How) Can this be achieved using Scala's reflection API? I am well aware that the signature of

What is “at” in shapeless (scala)?

拥有回忆 提交于 2019-11-29 01:17:36
问题 I've seen an object (probably a function) called "at" sprinkled throughout the shapeless source and in code that uses shapeless. In particular, it is used in the answer to this other question. Here is the code snippet: object iterateOverHList extends Poly1 { implicit def iterable[T, L[T] <: Iterable[T]] = at[L[T]](_.iterator) } I've had some clue that it is related to the apply method of the ~> type. What specifically does "at" do, and where is it defined? 回答1: Definition of PolyN#at at is a

Converting a tuple of options to an option of tuple with Scalaz or Shapeless

好久不见. 提交于 2019-11-28 23:17:16
Having (Some(1), Some(2)) I expect to get Some((1, 2)) and having (Some(1), None) I expect to get None You can use the fact that Scalaz 7 provides a Bitraverse instance for tuples and then sequence as usual (but with bisequence instead of sequence ): scala> import scalaz._, std.option._, std.tuple._, syntax.bitraverse._ import scalaz._ import std.option._ import std.tuple._ import syntax.bitraverse._ scala> val p: (Option[Int], Option[String]) = (Some(1), Some("a")) p: (Option[Int], Option[String]) = (Some(1),Some(a)) scala> p.bisequence[Option, Int, String] res0: Option[(Int, String)] = Some(

Different types in Map Scala

柔情痞子 提交于 2019-11-28 19:35:08
I need a Map where I put different types of values (Double, String, Int,...) in it, key can be String. Is there a way to do this, so that I get the correct type with map.apply(k) like val map: Map[String, SomeType] = Map() val d: Double = map.apply("double") val str: String = map.apply("string") I already tried it with a generic type class Container[T](element: T) { def get: T = element } val d: Container[Double] = new Container(4.0) val str: Container[String] = new Container("string") val m: Map[String, Container] = Map("double" -> d, "string" -> str) but it's not possible since Container

Passing a Shapeless Extensible Record to a Function

淺唱寂寞╮ 提交于 2019-11-28 18:22:17
I am trying to learn Shapeless (using version 2.10.2). I have created a very simple extensible record: val rec1 = ("foo" ->> 42) :: HNil According to the REPL, this has type shapeless.::[Int with shapeless.record.KeyTag[String("foo"),Int],shapeless.HNil] I am trying to define a simple function: def fun(x: ::[Int with KeyTag[String("foo"), Int], HNil]) = x("foo") but it does not even compile. I cannot use a String("foo") in the type declaration, and get an error. I have two questions: How can I specify the type of the extensible record in my code? When working with records with more fields, the

What does `T {}` do in Scala

我的梦境 提交于 2019-11-28 17:24:37
问题 Browsing Shapeless code, I came across this seemingly extraneous {} here and here: trait Witness extends Serializable { type T val value: T {} } trait SingletonOps { import record._ type T def narrow: T {} = witness.value } I almost ignored it as a typo since it does nothing but apparently it does something. See this commit: https://github.com/milessabin/shapeless/commit/56a3de48094e691d56a937ccf461d808de391961 I have no idea what it does. Can someone explain? 回答1: Any type can be followed by

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

早过忘川 提交于 2019-11-28 17:05:00
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,String("foo")],Int],shapeless.::[shapeless.record.FieldType[shapeless.tag.@@[Symbol,String("bar")],Int]

Passing a Shapeless Extensible Record to a Function (continued)

╄→尐↘猪︶ㄣ 提交于 2019-11-28 08:42:47
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](xs: L) = ... //Access to foo1, foo2, foo3 def fun2[L <: HList](xs: L) = ... //Access to foo1, foo2,