Questions tagged [shapeless]

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

shapeless
Filter by
Sorted by
Tagged with
274 votes
1 answer
7k views

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) ...
Roch's user avatar
  • 21.9k
151 votes
2 answers
5k views

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 ...
Rüdiger Klaehn's user avatar
150 votes
4 answers
20k views

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)...
Malte Schwerhoff's user avatar
113 votes
4 answers
13k views

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 ...
Ashkan Kh. Nazary's user avatar
74 votes
3 answers
14k views

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

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 ...
loyalflow's user avatar
  • 14.6k
53 votes
0 answers
1k views

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 } ...
beefyhalo's user avatar
  • 1,741
52 votes
1 answer
4k views

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] } ...
Travis Brown's user avatar
49 votes
5 answers
4k views

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 ...
Travis Brown's user avatar
48 votes
1 answer
2k views

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 ...
Edoardo Vacchi's user avatar
43 votes
3 answers
18k views

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) ...
Eldritch Conundrum's user avatar
42 votes
1 answer
1k views

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 ...
Erik Kaplun's user avatar
  • 37.5k
41 votes
1 answer
2k views

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 ...
pathikrit's user avatar
  • 33.1k
30 votes
4 answers
21k views

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) = ...
EECOLOR's user avatar
  • 11.2k
30 votes
3 answers
5k views

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 ...
Jesse Eichar's user avatar
  • 1,081
25 votes
5 answers
3k views

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 ...
Norbert Radyk's user avatar
24 votes
2 answers
3k views

Shapeless - turn a case class into another with fields in different order

I'm thinking of doing something similar to Safely copying fields between case classes of different types but with reordered fields, i.e. case class A(foo: Int, bar: Int) case class B(bar: Int, foo: ...
Utaal's user avatar
  • 8,534
24 votes
1 answer
3k views

Shapeless: Generic.Aux

I'm trying to understand how Generic works (and TypeClass too). The github wiki is very sparse on examples and documentation. Is there a canonical blog post / documentation page describing Generic and ...
Juanpa's user avatar
  • 667
24 votes
1 answer
6k views

how do i start learning shapeless concepts in scala [closed]

I would like to learn about polytypic concepts in Scala, I came across shapeless library what would be the best starting point for learning and applying shapeless.
prassee's user avatar
  • 3,641
22 votes
2 answers
2k views

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

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 ...
Travis Brown's user avatar
21 votes
1 answer
8k views

Getting subclasses of a sealed trait

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?
NietzscheanAI's user avatar
20 votes
1 answer
4k views

Converting Map[String,Any] to a case class using Shapeless

The question here asks about mapping a case class to a Map[String,Any]. I was wondering what would be the other way around, converting Map[String,Any] to a case class. Given the following map: val mp ...
Omid's user avatar
  • 1,969
19 votes
5 answers
10k views

Converting a tuple of options to an option of tuple with Scalaz or Shapeless

Having (Some(1), Some(2)) I expect to get Some((1, 2)) and having (Some(1), None) I expect to get None
Nikita Volkov's user avatar
19 votes
2 answers
3k views

How to define a function whose output type depends on the input type

Given the following classes: case class AddRequest(x: Int, y: Int) case class AddResponse(sum: Int) case class ToUppercaseRequest(str: String) case class ToUppercaseResponse(upper: String) How do I ...
jvliwanag's user avatar
  • 1,538
19 votes
2 answers
4k views

Converting nested case classes to nested Maps using Shapeless

I am trying to solve [this][1] 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) ...
Omid's user avatar
  • 1,969
19 votes
1 answer
4k views

Extract label values from a LabelledGeneric instance

Consider the following example: import shapeless._ case class Foo(bar: String, baz: Boolean) val labl = LabelledGeneric[Foo] Now, the type of labl is (prettified) LabelledGeneric[Foo] { type ...
Gabriele Petronella's user avatar
18 votes
7 answers
15k views

Different types in Map Scala

I need a Map where I put different types of values (Double, String, Int,...) in it, key can be String. Is there a way to do this, so that I get the correct type with map.apply(k) like val map: Map[...
pichsenmeister's user avatar
18 votes
2 answers
4k views

Understanding the Aux pattern in Scala Type System

This question may be asked and answered before, but I would like to understand this with an example and I could not reason out where the Aux pattern might be helpful! So here is the trait: trait Foo[...
joesan's user avatar
  • 14.6k
18 votes
2 answers
3k views

Passing a Shapeless Extensible Record to a Function

I am trying to learn Shapeless (using version 2.10.2). I have created a very simple extensible record: val rec1 = ("foo" ->> 42) :: HNil According to the REPL, this has type shapeless.::[Int ...
Eduardo's user avatar
  • 8,372
18 votes
2 answers
2k views

Sequencing an HList

Given a Shapeless HList where every list element shares the same type constructor, how can the HList be sequenced? For example: def some[A](a: A): Option[A] = Some(a) def none[A]: Option[A] = None ...
mpilquist's user avatar
  • 3,855
17 votes
1 answer
945 views

What are the important features of the shapeless API (in Scala), and what do they do?

I'm trying to learn shapeless (2.0.0). It seems like an amazing tool and I'm very excited about it, but I am having problems moving forward. Because there is not yet much documentation, I've been ...
jcrudy's user avatar
  • 4,011
16 votes
2 answers
2k views

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. ...
Travis Brown's user avatar
15 votes
2 answers
4k views

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 :: ...
Paul Draper's user avatar
  • 80.8k
15 votes
1 answer
2k views

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 ...
Ming's user avatar
  • 1,633
14 votes
1 answer
2k views

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 :...
ziggystar's user avatar
  • 28.5k
14 votes
3 answers
2k views

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 ...
jcrudy's user avatar
  • 4,011
14 votes
2 answers
909 views

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 ...
jwinandy's user avatar
  • 1,749
14 votes
1 answer
1k views

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" :...
emchristiansen's user avatar
14 votes
2 answers
1k views

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 ...
Stephen Woods's user avatar
13 votes
2 answers
4k views

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, ...
Fynn's user avatar
  • 4,823
13 votes
1 answer
1k views

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 ...
ziggystar's user avatar
  • 28.5k
13 votes
2 answers
3k views

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 = ...
mdedetrich's user avatar
  • 1,889
13 votes
1 answer
489 views

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 = ...
Brian McKenna's user avatar
13 votes
1 answer
966 views

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 ...
Sim's user avatar
  • 13.3k
12 votes
2 answers
2k views

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 ...
Giovanni Caporaletti's user avatar
12 votes
1 answer
1k views

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 ...
Taig's user avatar
  • 6,998
12 votes
1 answer
2k views

Mapping over Shapeless record

In a Play application I'm working on, I'm trying to improve our system for processing flags, some of which are meant to be persistent options as a user navigates our app via links. I'd like to use ...
acjay's user avatar
  • 35.6k
11 votes
2 answers
2k views

How to shapeless case classes with attributes and typeclasses?

I am currently implementing a library to serialize and deserialize to and from XML-RPC messages. It's almost done but now I am trying to remove the boilerplate of my current asProduct method using ...
Jorge's user avatar
  • 784
11 votes
2 answers
898 views

What is the purpose of the emptyCoproduct and coproduct methods of the TypeClass trait in Shapeless

It is not fully clear to me what is the purpose of the emptyCoProduct and coproduct methods of the TypeClass trait in Shapeless. When would one use the TypeClass trait instead of the ProductTypeClass?...
jedesah's user avatar
  • 3,013
11 votes
3 answers
2k views

Shapeless: map from coproduct to different coproduct

In the following, I'm trying to make a polymorphic function to convert a RawFeatureValue into a RefinedFeatureValue. import shapeless._ object test { type RawFeatureValue = Int :+: Double :+: ...
Utaal's user avatar
  • 8,534
11 votes
2 answers
2k views

Update case class from incomplete JSON with Argonaut or Circe

I need to create an updated instance from a case class instance (with any needed DecodeJsons implicitly derived), given an incomplete json (some fields missing). How can this be accomplished with ...
eirirlar's user avatar
  • 811

1
2 3 4 5
21