Questions tagged [shapeless]

shapeless is (an exploration of) a type class and dependent type based generic (aka polytypic/polymorphic) programming library for Scala.

shapeless is (an exploration of) a type class and dependent type based generic (aka polytypic/polymorphic) programming library for Scala derived from the various talks Miles Sabin has given over the course of 2011 on implementing scrap your boilerplate and higher rank polymorphism in Scala.

shapeless is an Open Source project under the Apache License v2.

1030 questions
10
votes
1 answer

How to iterate all the product types in a coproduct using shapeless?

Let's say I have a coproduct (a sealed trait) such as sealed trait Traity case object Foo extends Traity case class Bar() extends Traity case class Baz() extends Traity Using shapeless, I can apply polymorphic functions to specific instances but…
fommil
  • 5,757
  • 8
  • 41
  • 81
10
votes
2 answers

Scala, getting the type parameters of a KList as an HList

Suppose I have an abitrary KList, which for the sake of argument has type constructor Option[_], ie; type Example = Option[Int] :: Option[String] :: HNil Is there a way I can retrieve an Hlist made of the type parameters? type Params = Int ::…
J Pullar
  • 1,915
  • 2
  • 18
  • 30
10
votes
1 answer

what is Case.Aux in shapeless

I am confused about the example shown in the shapeless feature overview. object size extends Poly1 { implicit def caseInt = at[Int](x => 1) implicit def caseString = at[String](_.length) implicit def caseTuple[T, U] (implicit st :…
Xiaohe Dong
  • 4,953
  • 6
  • 24
  • 53
9
votes
2 answers

Mapped types in Scala

Is there a way to derive a type from an existing one in Scala? For example, for case class Person(name: String, age: Int) I'd like to get a Product/Tuple of (Option[String], Option[Int]), i.e. a type mapped from an existing one. There's a feature in…
Alan Thomas
  • 1,006
  • 2
  • 14
  • 31
9
votes
3 answers

How to create a random instance of a case class?

Suppose I've got a few case classes, e.g.: case class C(c1: Int, c2: Double, c3: Option[String]) case class B(b: Int, cs: Seq[C]) case class A(a: String, bs: Seq[B]) Now I would like to generate a few instances of A with random values for tests.…
Michael
  • 41,026
  • 70
  • 193
  • 341
9
votes
2 answers

Tagged Type vs class extends AnyVal

To introduce more type safety, we can either use tagged type provided by shapeless or create a class which extends AnyVal. What are the differences and advantage/disadvantage to use one over the other? Example: trait CountryCodeTag type CountryCode…
gyoho
  • 799
  • 2
  • 9
  • 25
9
votes
1 answer

How to generically extract field names with Shapeless?

Given a case class A I can extract its field names with Shapeless using the following snippet: val fieldNames: List[String] = { import shapeless._ import shapeless.ops.record.Keys val gen = LabelledGeneric[A] val keys =…
Matthias Langer
  • 994
  • 8
  • 22
9
votes
1 answer

how to show the type of a HList in scala shapeless

How do I get the type of a HList as a String so I can print it. eg "Int :: Long :: String :: HNil" val gen = Generic[?] val typeString: String = ??? println("The type is " + typeString) I know the String of it isnt very useful and usually you want…
Stephen
  • 4,228
  • 4
  • 29
  • 40
9
votes
3 answers

Cleaning up `case class` with `Option` FIelds

Given: case class Foo(a: Option[Int], b: Option[Int], c: Option[Int], d: Option[Int]) I'd like to only allow constructing a Foo only if at least one of its arguments is Some, i.e. not all fields are None. It would be quite a bit of code to write an…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
9
votes
1 answer

Constraint on HList: check for single occurrence of a type

I'm trying to add a constraint on an HList (from Shapeless): it should contain any arbitrary number of elements of type TA (from 0 to N); it should contain one and only one element of type TB. My example has this type hierarchy: trait T case class…
Alban Dericbourg
  • 1,616
  • 2
  • 16
  • 39
9
votes
1 answer

Scala vector of any dimension using shapeless

I need to measure distance in n-dimensional euclidean space, so I have to create multidimensional vectors and to be able to compare their dimensions and perform some basic operations like '+' or '-'. So, I thought I'll use type classes + shapeless…
Cauchy
  • 117
  • 6
9
votes
1 answer

Splitting an HList that was concatenated using Prepend[A, B]

I'm essentially looking for the opposite of the type class Prepend[A, B]. If I have something like: type A = String :: Int :: HNil type B = Boolean :: Double :: HNil val a: A = "a" :: 1 :: HNil val b: B = false :: 2.1 :: HNil scala> val ab = a ++…
Michael Zajac
  • 55,144
  • 7
  • 113
  • 138
9
votes
3 answers

Shapeless HList type checking

I am using Shapeless and have the following method to compute the difference between two HLists: def diff[H <: HList](lst1: H, lst2:H):List[String] = (lst1, lst2) match { case (HNil, HNil) => List() case (h1::t1, h2::t2) if…
triggerNZ
  • 4,221
  • 3
  • 28
  • 34
9
votes
1 answer

Evidence-preserving LUB constraint for HList

I think I need a HList that is constrained to have all of its elements being a subtype of a certain type. LUBConstraint seems to be what I want, and indeed it does constrain the construction of such a HList - but I can't see how to get the evidence…
Robin Green
  • 32,079
  • 16
  • 104
  • 187
9
votes
1 answer

Define a Typeclass for Shapeless Records

I'm trying to learn Shapeless, and I would like to define a monoid which adds together instances of shapeless records. Note that I'm using algebird monoids (not scalaz), but I'm sure they're quite similar. Here's an example of what I'd like to be…
JimN
  • 3,120
  • 22
  • 35