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
16
votes
2 answers

Refined and existential types for runtime values

Suppose I want to map between some strings and integer identifiers, and I want my types to make it impossible to get a runtime failure because someone tried to look up an id that was out of range. Here's one straightforward API: trait Vocab { def…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
15
votes
2 answers

Pattern matching with shapeless coproduct

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…
Paul Draper
  • 78,542
  • 46
  • 206
  • 285
15
votes
1 answer

Scala Function.tupled and Function.untupled equivalent for variable arity, or, calling variable arity function with tuple

I was trying to do some stuff last night around accepting and calling a generic function (i.e. the type is known at the call site, but potentially varies across call sites, so the definition should be generic across arities). For example, suppose I…
Ming
  • 1,613
  • 12
  • 27
14
votes
2 answers

Dynamically creating Akka Stream Flows at Runtime

I'm currently trying to dynamically create Akka Stream graph definitions at runtime. The idea being that users will be able to define flows interactively and attach them to existing/running BroadcastHubs. This means I don't know which flows or even…
14
votes
3 answers

What is "at" in shapeless (scala)?

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…
jcrudy
  • 3,921
  • 1
  • 24
  • 31
14
votes
1 answer

Extractor for a shapeless HList that mimics parser concatenation `~`

Question Is it somehow possible to create an extractor for shapeless' HList that looks like the following. val a ~ _ ~ b = 4 :: "so" :: 4.5 :: HNil => a == 4 && b == 4.5 Replace :: by ~, which shouldn't be the problem. Get rid of the terminating…
ziggystar
  • 28,410
  • 9
  • 72
  • 124
14
votes
2 answers

How to represent a partial update on case classe in Scala ?

I have the following case class : case class PropositionContent(title:String,content:String) And I would like to represent a partial modification of it as Data. One way would be to create the case class : case class…
jwinandy
  • 1,739
  • 12
  • 22
14
votes
1 answer

Creating an HList of all pairs from two HLists

I'm using shapeless in Scala, and I'd like to write a function allPairs that will take two HLists and return an HList of all pairs of elements. For example: import shapeless._ val list1 = 1 :: "one" :: HNil val list2 = 2 :: "two" :: HNil // Has…
emchristiansen
  • 3,550
  • 3
  • 26
  • 40
13
votes
1 answer

shapeless HList to TupleN where the tuple shape need not exactly match the HList shape

I would like to create the equivalent of: def toTupleN[A1, ..., AN, L <: HList](l: L): TupleN[A1, ..., AN] Code using toTupleN should compile only when there is exactly one N combination of values in l that the tuple could be created from. Anything…
Sim
  • 13,147
  • 9
  • 66
  • 95
13
votes
1 answer

Explain the `LowPriorityImplicits` pattern used in Scala type-level programming

When looking at the source of some Scala libraries, e.g. shapeless, I often find traits named LowPriorityImplicits. Can you please explain this pattern? What is the problem that is solved, and how does the pattern solve it?
ziggystar
  • 28,410
  • 9
  • 72
  • 124
13
votes
1 answer

Proving associativity of natural number addition using Scala shapeless

The following code is Idris: natAssociative : (a : Nat) -> (b : Nat) -> (c : Nat) -> (a + b) + c = a + (b + c) natAssociative Z b c = the (b + c = b + c) refl natAssociative (S k) b c = replace {P=\x => S (k + b) + c = S x} (natAssociative k b c)…
Brian McKenna
  • 45,528
  • 6
  • 61
  • 60
13
votes
2 answers

Safely copying fields between case classes of different types

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 =…
mdedetrich
  • 1,899
  • 1
  • 18
  • 29
13
votes
2 answers

Type casting using type parameter

Given is a Java method that returns java.lang.Objects 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…
Fynn
  • 4,753
  • 3
  • 32
  • 69
12
votes
2 answers

How to get the name of a case class field as a string/symbol at compile time using shapeless?

I'd like to somehow get at compile time the name of a field of a case class in a val (possibly a singleton-typed string or symbol?). Something like the following: import shapeless._ case class MyClass(field1: String, field2: Int) val field1Lens =…
Giovanni Caporaletti
  • 5,426
  • 2
  • 26
  • 39
12
votes
1 answer

Using Slick with shapeless HList

Slick's support for HList is generally a great thing. Unfortunately, it comes with its own implementation that does barely provide any useful operations. I'd therefore like to use the shapeless HList instead. This is supposed to be "trivial", but I…
Taig
  • 6,718
  • 4
  • 44
  • 65
1 2
3
68 69