Questions tagged [shapeless]
shapeless is (an exploration of) a type class and dependent type based generic (aka polytypic/polymorphic) programming library for Scala.
1,025
questions
1
vote
0
answers
54
views
Should tagged types work for value classes?
I am trying to create a tagged type (in Scala 2.x) in the same way that Shapeless (2.x) does but it fails with a ClassCastException:
object Main {
sealed trait TaggedProps[Props] extends Any
...
2
votes
1
answer
80
views
"Distributive property" with Shapeless
Not sure if the correct term is "distributive property" but I remember learning this in school so here's an example of what I'm trying to do:
Given:
type MyHList = (A :+: B :+: C :+: CNil) ::...
1
vote
1
answer
50
views
How to zip values with types? Shapeless
I have a HList of coproducts that looks like this:
(A :+: B :+: CNil) :: (Foo :+: Bar :+: CNil) :: HNil
This HList will be a value in my program. E.g.,:
val myhlist: (A :+: B :+: CNil) :: (Foo :+: ...
3
votes
2
answers
125
views
Converting a stream of coproduct to a stream of HList - Shapeless and FS2
I have a fs2 Stream Stream[F, C] where C <: Coproduct. And I want to transform it into a Stream[F, H] where H <: HList. This HList should contain all members that the coproduct C had.
So, ...
1
vote
2
answers
85
views
Why the Scala compiler can provide implicit outside of object, but cannot inside?
The title might be quite vague, but here is the code: https://github.com/amorfis/why-no-implicit
So there is a tool to transform Map[String, Any] to a simple case class. The tests pass and this piece ...
1
vote
1
answer
92
views
How to derive a Generic.Aux if the case class has a type parameter - Shapeless
given:
sealed trait Data
final case class Foo() extends Data
final case class Bar() extends Data
final case class TimestampedData[A <: Data](data: A, timestamp: Long)
Is there a succint way to ...
1
vote
1
answer
116
views
Shapeless3 and annotations
This is a followup on Shapeless and annotations. The original question was asked in the context of Scala 2 and Shapeless2.
Some features from Shapeless2 were migrated to Shapeless3, such as ...
0
votes
1
answer
49
views
SYB `cast` function in Scala
I am reading the Scrap Your Boilerplate paper and trying to follow along by implementing the ideas in scala as best I can. However, I'm stuck on the very first function, the cast, which is used to ...
1
vote
1
answer
113
views
Does shapeless for Scala 2 has analogue for Scala 3 union types?
Scala 3 has very convenient Union types. I'm looking for something closer in shapeless for Scala 2 that would allow similar behavior. Example:
case class Cs1()
case class Cs2()
I tried shapeless.v, ...
0
votes
1
answer
126
views
How to accept only a specific subtype of existential type?
Consider the following implementation sketch:
sealed trait Type
object Type {
case object Type1 extends Type
case object Type2 extends Type
}
sealed trait Data {
type T <: Type
}
object ...
1
vote
0
answers
61
views
How to compose, wrapped and parameterised Function1 instances using Shapeless library
I am trying to build a composer utility for a range of WorkItems, based on Shapeless HList
class WorkItem[-A, B](name: String)(val work: A => B) {
// some additional methods
def andThen[C](...
0
votes
1
answer
52
views
Implicits not found when mapping a shapeless HList with a natural transformation
I'm manipulating shapeless records (hlists with field labels) of values of type Param[A]. Each of them can generate an upper bound of type ParamMatcher[A] and I've written this poly function to ...
0
votes
1
answer
57
views
Struggling with implicit resolution when trying to pass a generic class to my csvloader
I'm a little baffled as to why this doesn't work...
To learn shapeless I'm implementing everyone's favorite csv loader. It's largely working, I can do things like
val caseClasses = CSVConverter[...
1
vote
1
answer
121
views
Does shapeless play well with type members?
I'm trying to use shapeless to derive a Generic for a type member defined in a trait, but am not having luck. I have made as simple of a reproduction of the issue as I can think of while keeping it ...
1
vote
3
answers
119
views
Is it possible to control the number of argument in a function when using varargs in compile time(Scala)?
Let us suppose, we have to create the OddList[+T] which contains only odd number of elements.
Now can we do something like this
OddList(1,2) //Works fine
OddList(1,2,3) //Compilation error
if there ...
1
vote
2
answers
104
views
How to flatten an HLists of HLists
So at runtime I get an Hlist of Hlists which looks like:
(2 :: HNil) :: (1001 :: HNil) :: (1001 :: HNil) :: HNil
Here the type of the resultant Hlist is :
(Int :: HNil) :: (Long :: HNil) :: (Long :: ...
1
vote
1
answer
78
views
Scala Shapeless No Implicits Found Case Class Migration Adding New Field
In order to get familiar with shapeless, I did investigate into the examples of the shapeless-guide. I am especially interested in case class migrations. Therefore, I did dive into the case study of ...
2
votes
1
answer
60
views
Using K0.ProductInstances in shapeless3
I have a library with typeclasses that I am migrating to Scala 3 using shapeless-3. One of my typeclasses is:
trait Parser[T] {
def parse(ctx: Context): (Option[T], Context)
}
where Option[T] ...
0
votes
2
answers
173
views
Get case class parameter types as a HList
I'm trying to generate instances of case class using shapeless
This works for generating instances of Foo
case class Foo(x: Int, y: String)
class Context {
val random = new Random()
}
def genInt(...
0
votes
1
answer
47
views
HList(DValue[A], DValue[B]) to HList(A, B) at library level?
I'm building a data binding library, which has 3 fundamental classes
trait DValue[+T] {
def get:T
}
class DField[T] extends DValue[T] {
// allow writes + notifying observers
}
class DFunction[T](...
0
votes
0
answers
62
views
Generically turn JsObject into specific instances of a trait for Slick using Shapeless (and spray-json)
I have (what I think is) an interesting problem trying to generically map sealed trait instances to and from a single Slick table. I have something like the following models:
sealed trait Base {
...
2
votes
1
answer
88
views
List null fields recursively with Shapeless
I'm trying to list null fields recursively with Shapeless. But it doesn't seem to show all the fields:
https://scastie.scala-lang.org/PtLdSRC2Qfipu054Hzerrw
import shapeless._
import shapeless....
0
votes
1
answer
89
views
Getting predicate from Refined
Is it possible to extract the bounds from the predicate/witness of a Refined variable at runtime? Something like the following.
// Should return L as a Double
def getLowerBound[L, H](v: Refined[Double,...
2
votes
1
answer
55
views
why I cannot use FieldType as a type in a val
I was reading chapter 5.2 Type tagging and phantom types of https://books.underscore.io/shapeless-guide/shapeless-guide.html#fn4
and I tried this:
val tmp: FieldType["foo", Int] = "...
1
vote
0
answers
49
views
Unable to derive typeclass instance for Coproduct using shapeless in scala 2.11.8
I have a generic XML writer like below (minimal code to reproduce the issue):
import shapeless.labelled.FieldType
import shapeless._
import scala.language.{implicitConversions, postfixOps}
import ...
1
vote
1
answer
37
views
How to turn an HList of Const functors into a Seq?
I have a higher-kinded data type like this
case class Foo[F[_] : Functor](a: F[Int], b: F[Double])
and a value that labels every member with some kind of string (the name of a CSV column for example):...
1
vote
1
answer
55
views
Is it possible to write a "zipAll" function for scala inductive list (shapeless.HList in scala 2 or Tuple in scala 3)?
To this date, the "zip" function in HList works like zip function or untyped List in scala. It will discard elements from the longer operand to be conformant to the shorter element:
val a = ...
1
vote
0
answers
58
views
Shapeless 2.3.5+ can't provide implicit for Generic.Aux
After updating to Shapeless 2.3.5+ code that used to work stopped compiling. I boiled it down to a minimal, yet still somewhat complicated example:
import shapeless._
trait A
trait B[+A1 <: A]
...
1
vote
1
answer
63
views
Shapeless generic ZipWith with static poly
I have some code:
case class Name(name: String) extends StaticAnnotation
case class Example(@Name("baz") foo: String, bar: Int)
object nameAnnotationZip2 extends Poly2 {
implicit val ...
1
vote
1
answer
55
views
How can you access annotations on a field when generating LabelledGeneric?
Is there a way to tag a generated shapeless LabelledGeneric with information about annotations on fields?
For example
case class Example(@someAnnotation foo: Foo, bar: Bar)
could we get something ...
2
votes
2
answers
98
views
How to compare types of different, generic HLists?
So I have a user defined input of keys, and the user is also expected to provide serializers for these keys. I am trying to statically ensure that the serializers provided work with the user provided ...
1
vote
1
answer
95
views
Scala typeclass for providing an instance either from derivation or from an existing implicit value
I'm getting started with generic programming in Scala and I'm trying to design a flexible buildable schema type (a generic description of ADTs) that can be translated into third party serialization (e....
2
votes
1
answer
136
views
Transform/traverse Shapeless' HMap
Shapeless' HListOps includes a number of useful functions for their heterogeneous HList type. I couldn't find an equivalent for HMap.
Here is my goal. I have a simple Map[String, String] which is used ...
0
votes
1
answer
185
views
How to match a Case Class containing a Parameter with Generic Type
I have an interesting Problem matching a Case Class in Scala....
I am using Akka and I have functionality that I will use in every Actor in my System, so created a Base Class for my Actor and I try to ...
0
votes
1
answer
122
views
How to get Option[T] from object using Shapeless
I was trying to get Option[Author] from book in getAuthor method:
import shapeless.ops.record._
case class Book(title: String, author: Option[Author])
case class Author(name: String)
val book ...
1
vote
1
answer
90
views
HMap with case objects as keys
I am trying to achieve something with Shapeless' HMap, but am not sure whether this is possible and if it is, then how would I go about it.
I want to use HMap as a map of user preferences where values ...
1
vote
1
answer
127
views
How to reduce boilerplate code with Scala Macros in Scala 2?
For such code, there are many boilerplate code.
object TupleFlatten {
import shapeless._
import ops.tuple.FlatMapper
import syntax.std.tuple._
trait LowPriorityFlat extends Poly1 {
...
2
votes
1
answer
195
views
How to flat nest tuple parameter in function?
I have a function g which need parameter (Int, (Int, Int)) => Int, and a flat function f0 (Int, Int, Int) => Int
I want to construct a function ft which can flat parameters of g to f0.
Here is ...
0
votes
4
answers
158
views
Can a make a Scala trait with a polymorphic, variable-arity abstract method?
Say I have a Scala trait that does some computation and then calls a polymorphic method on extending classes that might have a different method signature in each class:
trait GenericThing {
val ...
0
votes
0
answers
125
views
Shapeless' Lazy and implicit parameter
I am trying to trace the sequence of construction (lots of println) and have this code
https://scastie.scala-lang.org/4DRXuhUZS8mN551eI7vN3Q
import shapeless._
object abc {
sealed trait List[+T]
...
0
votes
0
answers
86
views
shapeless.~?>.witness[Nothing, Nothing, T](shapeless.~?>.rel[Nothing, Nothing])when upgrading scala version from 2 to 3
I'm upgrading my scala project from version 2 to 3.
This is my build.sbt for version scala version 2
scalaVersion := "2.13.5"
version := "1.0.0"
libraryDependencies += ...
2
votes
1
answer
303
views
Getting MirroredElemLabels from Mirror
Scala 3's scala.deriving.Mirror has a type member MirroredElemLabels which is a tuple of string literals. What's the standard way to get that type as a value?
EDIT: here's the code that produces a ...
1
vote
1
answer
129
views
Implicit parameter precedence
I was trying to convert generic type to HList:
trait ToHList[T] {
type Out <: HList
def apply(value: T): Out
}
trait LowPriorityToHList {
implicit def default[T]: ToHList.Aux[T, T :: HNil] =
...
3
votes
1
answer
118
views
How to concatenate function arguments and return values using shapeless
I was trying to define compose method below:
import shapeless.{::, HList, HNil}
def compose[A1 <: HList, R1, A2 <: HList, R2](f: A1 => R1, g: A2 => R2): R1 :: R2 :: HNil = ???
val f = (...
1
vote
1
answer
398
views
Could not find implicit value for parameter Mapper
I try to create a simple shapeless based function to convert the case class into a list of string I could then encode as csv.
The point of this is to summon type class CsvEncoder on every member of ...
2
votes
1
answer
886
views
How to get Scala case class fields and values as (String, String) with Shapeless or Macro
I have been struggling for a couple days already to try to create a macro or use shapeless to create a method/function to extract field names and values as a Tuple[String, String].
Lets imagine the ...
1
vote
1
answer
155
views
How to get the name of a class as a string literal at compile time using shapeless?
This is a follow-up question of:
How to get the name of a case class field as a string/symbol at compile time using shapeless?
Assuming that I want to write a recursive converter that can convert a ...
0
votes
1
answer
54
views
Singleton type as a type member
I'm trying to define a type member to be a singleton type. Here is my attempt:
import shapeless.syntax.singleton._
trait Test{
type Blocked <: Boolean
}
def f(t: Test{ type Blocked = false....
2
votes
1
answer
126
views
How to obtain all possible members of a coproduct
I've been trying to list all the members of a coproduct type. This answer is really close to what I want to achive:
sealed trait Traity
case object Foo extends Traity
case class Bar() extends Traity
...
0
votes
1
answer
157
views
Shapeless: extract case case field value parameterized by annotation
I am trying to generate a typeclass for returning the value of a case class field that has a particular annotation at compile time, using shapeless.
That's given a Scala annotation case class and a ...