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
274
votes
1 answer

How to use Shapeless in a Quasiquote?

I'm trying to call a Shapeless macro from inside a quasiquote with Scala and I'm not getting what I would like to get. My macro doesn't return any errors but it doesn't expand Witness(fieldName) into Witness.Lt[String] val implicits =…
Roch
  • 21,741
  • 29
  • 77
  • 120
151
votes
2 answers

Limits of Nat type in Shapeless

In shapeless, the Nat type represents a way to encode natural numbers at a type level. This is used for example for fixed size lists. You can even do calculations on type level, e.g. append a list of N elements to a list of K elements and get back a…
Rüdiger Klaehn
  • 12,445
  • 3
  • 41
  • 57
151
votes
4 answers

Are HLists nothing more than a convoluted way of writing tuples?

I am really interested in finding out where the differences are, and more generally, to identify canonical use cases where HLists cannot be used (or rather, don't yield any benefits over regular lists). (I am aware that there are 22 (I believe)…
Malte Schwerhoff
  • 12,684
  • 4
  • 41
  • 71
113
votes
4 answers

Any reason why scala does not explicitly support dependent types?

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…
Ashkan Kh. Nazary
  • 21,844
  • 13
  • 44
  • 68
74
votes
3 answers

Can someone explain to me what the Shapeless library is for?

Can someone explain to me in simple terms what the Shapeless library is for? Scala has generics and inheritance functionality so I'm a bit confused what Shapeless is for. Maybe a use case to clarify things would be helpful.
loyalflow
  • 14,275
  • 27
  • 107
  • 168
52
votes
1 answer

Can't prove that singleton types are singleton types while generating type class instance

Suppose I've got a type class that proves that all the types in a Shapeless coproduct are singleton types: import shapeless._ trait AllSingletons[A, C <: Coproduct] { def values: List[A] } object AllSingletons { implicit def cnilSingletons[A]:…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
49
votes
5 answers

Testing an assertion that something must not compile

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…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
48
votes
1 answer

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

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…
Edoardo Vacchi
  • 1,284
  • 10
  • 18
48
votes
0 answers

Materialize the Value for a Type that has One Inhabitant

Thanks to @MilesSabin's answer I can write a type level Fibonacci sequence: sealed trait Digit case object Zero extends Digit case object One extends Digit sealed trait Dense { type N <: Dense } sealed trait DNil extends Dense { type N = DNil…
beefyhalo
  • 1,691
  • 2
  • 21
  • 33
44
votes
3 answers

Use functional combinators on Scala Tuples?

'map' preserves the number of elements, so using it on a Tuple seems sensible. My attempts so far: scala> (3,4).map(_*2) error: value map is not a member of (Int, Int) (3,4).map(_*2) ^ scala>…
Eldritch Conundrum
  • 8,452
  • 6
  • 42
  • 50
42
votes
1 answer

Using the "Prolog in Scala" to find available type class instances

Considering https://speakerdeck.com/folone/theres-a-prolog-in-your-scala, I would like to "abuse" the Scala type system to find all instances of e.g. CanBuildFrom that match a given criteria. Prolog style, I would evaluate something in the lines of…
41
votes
1 answer

What does `T {}` do in Scala

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…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
30
votes
3 answers

Can Map be performed on a Scala HList

I have done a few implementations of HList now. One based on Daniel Spiewak's High Wizardry in the Land of Scala talk and another based on a post in Apocalisp blog. The goal was to have a heterogenous list of which is not heterogenous in the…
Jesse Eichar
  • 1,081
  • 1
  • 11
  • 11
30
votes
4 answers

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

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
EECOLOR
  • 11,184
  • 3
  • 41
  • 75
25
votes
5 answers

Type based collection partitioning in Scala

Given the following data model: sealed trait Fruit case class Apple(id: Int, sweetness: Int) extends Fruit case class Pear(id: Int, color: String) extends Fruit I've been looking to implement a segregate basket function which for the given…
Norbert Radyk
  • 2,608
  • 20
  • 24
1
2 3
68 69