shapeless

How to require typesafe constant-size array in scala?

可紊 提交于 2019-11-27 03:27:07
问题 I need something like this: def encryptBlock(arr: FixedArray[Size16]) = ??? val blocks = arr.splitFixed[Size16] val encrypted = encryptBlock(FixedArray[Size16]()) So, to be sure that I receive only 128-bit array as input. 回答1: Shapeless can do that for seqs: import shapeless._ import nat._ import syntax.sized._ scala> def func(l: Sized[List[Int], _3]) = l func: (l: shapeless.Sized[List[Int],shapeless.nat._3])shapeless.Sized[List[Int],shapeless.nat._3] scala> List(1,2,3,4,5,6).grouped(3).map(_

Safely copying fields between case classes of different types

旧城冷巷雨未停 提交于 2019-11-27 01:46:30
问题 Assuming you have case classes like the following case class Test1(a:String,b:Int,c:Char) case class Test2(a:String,b:Int) And you instantiate the classes with the following variables val test1 = Test1("first",2,'3') val test2 = Test2("1st",20) Is there a way to use the .copy method (or otherwise), to apply the variables inside Test2 to Test1, something like val test3 = test1.copy(test2) //Note this isn't valid scala code // Result should be ("1st",20,'3') If this isn't possible in pure scala

Pattern matching with shapeless coproduct

吃可爱长大的小学妹 提交于 2019-11-27 01:22:52
问题 Can I use pattern matching with shapeless coproducts? import shapeless.{CNil, :+:} type ListOrString = List[Int] :+: String :+: CNil def f(a: ListOrString): Int = a match { case 0 :: second :: Nil => second case first :: Nil => first case Nil => -1 case string: String => string.toInt } That of course doesn't work, since a is boxed as a Coproduct . Is there an alternative way to use coproducts and maintain the ability to pattern match? 回答1: You can use the Inl and Inr constructors in the

How to append or prepend an element to a tuple in Scala

…衆ロ難τιáo~ 提交于 2019-11-27 01:16:18
问题 I have a tuple and want to add an element without loosing type safety. This is what I want to achieve: val tuple = ("", 1, 1f) // (String, Int, Float) val newTuple:(String, Int, Float, Double) = tuple :+ 1d 回答1: It's worth noting that you can also write a code generator for this in a few lines: val tupadd = for (n <- 2 to 20) yield { val t = (0 until n).map(i => ('A'+i).toChar).mkString(", ") val u = ('A'+n).toChar val i = (0 until n).map(i => "x._"+(i+1)).mkString(", ") List( s"implicit

Testing an assertion that something must not compile

安稳与你 提交于 2019-11-26 22:33:09
问题 The problem When I'm working with libraries that support type-level programming, I often find myself writing comments like the following (from an example presented by Paul Snively at Strange Loop 2012): // But these invalid sequences don't compile: // isValid(_3 :: _1 :: _5 :: _8 :: _8 :: _2 :: _8 :: _6 :: _5 :: HNil) // isValid(_3 :: _4 :: _5 :: _8 :: _8 :: _2 :: _8 :: _6 :: HNil) Or this, from an example in the Shapeless repository: /** * If we wanted to confirm that the list uniquely

Getting subclasses of a sealed trait

时光总嘲笑我的痴心妄想 提交于 2019-11-26 16:05:33
问题 Is it possible (via macros, some form of Shapeless automagic or otherwise) to obtain a list of the subclasses of a sealed trait: At compile time? At runtime? 回答1: You don't need any 3rd party library to do this: sealed trait MyTrait case object SubClass1 extends MyTrait case object SubClass2 extends MyTrait import scala.reflect.runtime.{universe => ru} val tpe = ru.typeOf[MyTrait] val clazz = tpe.typeSymbol.asClass // if you want to ensure the type is a sealed trait, // then you can use clazz

Why is the Aux technique required for type-level computations?

点点圈 提交于 2019-11-26 15:40:10
问题 I'm pretty sure I'm missing something here, since I'm pretty new to Shapeless and I'm learning, but when is the Aux technique actually required ? I see that it is used to expose a type statement by raising it up into the signature of another "companion" type definition. trait F[A] { type R; def value: R } object F { type Aux[A,RR] = F[A] { type R = RR } } but isn't this nearly equivalent to just putting R in the type signature of F ? trait F[A,R] { def value: R } implicit def fint = new F[Int

Any reason why scala does not explicitly support dependent types?

不打扰是莪最后的温柔 提交于 2019-11-26 14:59:12
问题 There are path dependent types and I think it is possible to express almost all the features of such languages as Epigram or Agda in Scala, but I'm wondering why Scala does not support this more explicitly like it does very nicely in other areas (say, DSLs) ? Anything I'm missing like "it is not necessary" ? 回答1: Syntactic convenience aside, the combination of singleton types, path-dependent types and implicit values means that Scala has surprisingly good support for dependent typing, as I've

Converting nested case classes to nested Maps using Shapeless

≡放荡痞女 提交于 2019-11-26 13:18:43
问题 I am trying to solve this question using Shapeless, in summary it's about converting a nested case class to Map[String,Any], here is the example: case class Person(name:String, address:Address) case class Address(street:String, zip:Int) val p = Person("Tom", Address("Jefferson st", 10000)) It wants to convert p to following: Map("name" -> "Tom", "address" -> Map("street" -> "Jefferson st", "zip" -> 10000)) I am trying to do it using Shapeless LabelledGeneric , here is what I for so far:

Weird behavior trying to convert case classes to heterogeneous lists recursively with Shapeless

别来无恙 提交于 2019-11-26 09:40:27
问题 I stayed up way too late last night trying to figure out this Shapeless issue and I\'m afraid it\'s going to eat my evening if I don\'t get it off my chest, so here goes. In this minimized version I\'m just defining a type class that will recursively convert case classes into heterogeneous lists: import shapeless._ trait DeepHLister[R <: HList] extends DepFn1[R] { type Out <: HList } trait LowPriorityDeepHLister { type Aux[R <: HList, Out0 <: HList] = DeepHLister[R] { type Out = Out0 }