shapeless

When calling a scala function with compile-time macro, how to failover smoothly when it causes compilation errors?

一世执手 提交于 2021-02-13 05:43:30
问题 Assuming that I intend to use the singleton/literal type feature in a scala program, this feature is provided in shapeless library in scala 2.12 (scala 2.13 supports native literal type but let's use shapeless as an example) In shapeless, literal type is represented as a path-dependent inner type of Witness object, which can be implicitly converted from a scala literal/const: import com.tribbloids.spike.BaseSpec import shapeless.Witness import scala.util.Random val w: Witness.Lt[Int] = 3 val

Can shapeless Record type be used as a Poly1?

纵饮孤独 提交于 2021-02-11 12:22:03
问题 Assuming if I have the following Record typed data, and a hlist of keys: val rr = ("a" ->> 1) :: ("b" -> "s") :: ("c" -> 3) :: HNil val hh = "c" :: "b" :: HNil And I want to extract values in rr for each key in hh , then combine them into a type level object, eventually yielding: (3: Int) :: ("s": String) :: HNil How this can be achieved with least amount of code? I could obviously write a inductively-summoned implicit function but it seems to be overkill 回答1: Firstly, you have typos. ->>

Can shapeless Record type be used as a Poly1?

限于喜欢 提交于 2021-02-11 12:21:58
问题 Assuming if I have the following Record typed data, and a hlist of keys: val rr = ("a" ->> 1) :: ("b" -> "s") :: ("c" -> 3) :: HNil val hh = "c" :: "b" :: HNil And I want to extract values in rr for each key in hh , then combine them into a type level object, eventually yielding: (3: Int) :: ("s": String) :: HNil How this can be achieved with least amount of code? I could obviously write a inductively-summoned implicit function but it seems to be overkill 回答1: Firstly, you have typos. ->>

HList foldLeft with tuple as zero

僤鯓⒐⒋嵵緔 提交于 2021-02-10 15:46:48
问题 I'm trying to foldLeft on a HList with an accumulator of type (HL, Int) , where HL is a HList. The program below does not compile. However, if I switch to a simpler accumulator of type HL (by just switching the commented lines with the ones above), it compiles and it works. Wrapping an HList in a tuple breaks the implicit resolution for the leftFolder. What am I missing? package foo.bar import shapeless.{:+:, ::, CNil, Coproduct, Generic, HList, HNil, Lazy, Poly2} import shapeless.ops.hlist.

Finding the second matching implicit

☆樱花仙子☆ 提交于 2021-02-10 13:25:40
问题 Consider the following setup: trait Foo[A] object Foo extends Priority2 trait Priority0 { implicit def foo1: Foo[Int] = new Foo[Int] {} } trait Priority1 extends Priority0 { implicit def foo2: Foo[Boolean] = new Foo[Boolean] {} } trait Priority2 extends Priority1 { implicit def foo3: Foo[Double] = new Foo[Double] {} } Now, in a REPL (having loaded the above code up), I can do the following: scala> def implicitlyFoo[A](implicit foo: Foo[A]) = foo implicitlyFoo: [A](implicit foo: Foo[A])Foo[A]

In scala 2.13, why is it sometimes impossible to summon type class explicitly?

牧云@^-^@ 提交于 2021-02-08 04:42:13
问题 Here is a simple example in shapeless 2.3.3: val book = ("author" ->> "Benjamin Pierce") :: ("title" ->> "Types and Programming Languages") :: ("id" ->> 262162091) :: ("price" ->> 44.11) :: HNil val v1 = book.values assert(v1.head == "Benjamin Pierce") // works fine // summoning Values[_] type class explicitly, the HList & TypeTag are optional case class HasValues[T <: HList: TypeTag](v: T) { def vs(implicit v: Values[T]): Values[T] = v } val _vs = HasValues(book).vs val v2 = book.values(_vs)

Prohibit generating of apply for case class

血红的双手。 提交于 2021-02-07 02:53:30
问题 I'm writing a type-safe code and want to replace apply() generated for case class es with my own implementation. Here it is: import shapeless._ sealed trait Data case object Remote extends Data case object Local extends Data case class SomeClass(){ type T <: Data } object SomeClass { type Aux[TT] = SomeClass { type T = TT } def apply[TT <: Data](implicit ev: TT =:!= Data): SomeClass.Aux[TT] = new SomeClass() {type T = TT} } val t: SomeClass = SomeClass() // <------------------ still compiles,

Automatically convert a case class to an extensible record in shapeless?

走远了吗. 提交于 2021-02-06 09:12:37
问题 If I have these two case classes: case class Address(street : String, zip : Int) case class Person(name : String, address : Address) and an instance: val person = Person("Jane", Address("street address", 12345)) Is there a way in shapeless to automatically convert person to an extensible record? I am interested in both shallow and deep conversions. The shallow copy would be something like: 'name ->> "Jane" :: 'address ->> Address("street address", 12345) :: HNil In the deep conversion, the

How to implicitly figure out the type at the head of a shapeless HList

空扰寡人 提交于 2021-02-05 12:04:49
问题 Lets say I have the following: case class TestField(value: String) case class TestField2(value: String) implicit class ProductExtensions[T <: Product](val value T) extends AnyVal { def mapTo[R <: Product](implicit tGen: Generic.Aux[T, String :: HNil], rGen: Generic.Aux[R, String :: HNil]: R = ??? } val testField2 = TestField("my value").mapTo[TestField2] // TestField2("my value") Can I "genersize" the mapTo function to work for types other than String without having to specify the type? Note

How to implicitly figure out the type at the head of a shapeless HList

佐手、 提交于 2021-02-05 12:03:40
问题 Lets say I have the following: case class TestField(value: String) case class TestField2(value: String) implicit class ProductExtensions[T <: Product](val value T) extends AnyVal { def mapTo[R <: Product](implicit tGen: Generic.Aux[T, String :: HNil], rGen: Generic.Aux[R, String :: HNil]: R = ??? } val testField2 = TestField("my value").mapTo[TestField2] // TestField2("my value") Can I "genersize" the mapTo function to work for types other than String without having to specify the type? Note