Questions tagged [shapeless]
shapeless is (an exploration of) a type class and dependent type based generic (aka polytypic/polymorphic) programming library for Scala.
1,005
questions
2votes
1answer
62views
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....
0votes
1answer
44views
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,...
2votes
0answers
28views
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] = "...
1vote
0answers
25views
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 ...
1vote
1answer
28views
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):...
1vote
1answer
33views
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 = ...
1vote
0answers
35views
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]
...
1vote
1answer
24views
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 ...
1vote
1answer
31views
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 ...
2votes
1answer
61views
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 ...
1vote
0answers
40views
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....
2votes
1answer
67views
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 ...
0votes
1answer
126views
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 ...
0votes
1answer
57views
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 ...
1vote
1answer
61views
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 ...
1vote
1answer
71views
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 {
...
2votes
1answer
117views
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 ...
0votes
4answers
90views
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 ...
0votes
0answers
55views
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]
...
0votes
0answers
50views
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 += ...
1vote
1answer
141views
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 ...
1vote
1answer
83views
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] =
...
3votes
1answer
84views
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 = (...
1vote
1answer
151views
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 ...
2votes
1answer
123views
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 ...
0votes
0answers
177views
issue in the decode of a list of various types with circe
I have a json string which is a list of json objects, and each json has several embedded objects; the main problem is in
custom_fields
, this field is a list and it can have several types of objects; ...
1vote
1answer
97views
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 ...
0votes
1answer
38views
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....
2votes
1answer
62views
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
...
0votes
1answer
98views
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 ...
1vote
1answer
142views
Shapeless: Could not find implicit value for updater
I am working on a side project, trying to implement immutable aggregates in Scala. My idea is to have base trait AggregateRoot with some common behaviors. Child classes would be actual aggregates ...
3votes
1answer
109views
Extract a subset of a tuple in Scala 3
Given an arbitrary tuple, I want to extract a subset of the tuple.
The signature would look something like:
def subset[T1 <: Tuple, T2 <: Tuple](t:T1): T2 = ???
where T2 is a tuple with some ...
0votes
1answer
73views
In scala shapeless, how to use Record.updateWith inside another method?
From what I have read online, the shapeless Record.updateWith seems to be the only way to update both the value & type of a Record entry. So I gave it a try:
import shapeless.syntax.singleton....
4votes
0answers
88views
How can I prevent Scala from satisfying this implicit's parameters with itself, even though it's a different type?
I'm trying to make a generic decoder that decodes fields differently depending on shapeless.Tags declared on those fields. To that end, I had an implicit defined as follows which can turn any decoder ...
0votes
1answer
32views
Shapeless cannot provide implicit instance of Annotations.Aux
I'm trying to simplify annotations extracting from case classes, but I still want to stay as generic as possible. So, I've created a simple type class that does nothing useful (for now), but it ...
1vote
0answers
57views
Is there a way to find arg values reliably from case class which is used as annotation?
I am working in Scala programming language. We are using annotations to apply some metadata on class fields
e.g.
Annotation class
case class Address(city: City, zip: Zip) extends StaticAnnotation //...
1vote
1answer
39views
Expanding Option[_] over a Product in Shapeless
I'm learning my way through Shapeless, and I have a specific behavior I'm looking to implement to allow our code to expand out optional case classes identically whether they're present or not. ...
0votes
2answers
99views
Is a polymorphic function of higher-kind C[_] ~> D[_] really a natural transformation?
This is a question about terminology, in scala shapeless library I found the following comment:
/**
* Base trait for natural transformations.
*
* @author Miles Sabin
*/
trait ~>[F[_]...
2votes
1answer
80views
How do I map over an HList where all of the elements are instances of a typeclass?
Let's say I have a typeclass such as this one:
trait Select[A] {
def select(selector: String): Set[A]
}
The typeclass provides the functionality "given a selector string, gimme a set of A ...
0votes
1answer
107views
Can shapeless Record type be used as a Poly1? - Part 2
Since I didn't get an answer from the Part 1:
Can shapeless Record type be used as a Poly1?
I assume this feature doesn't exist in shapeless. So I decided own my fate and to write one myself:
import ...
0votes
1answer
51views
Can shapeless Record type be used as a Poly1?
Assuming if I have the following Record typed data, and a hlist of keys:
val rr = ("a" ->> 1) ::
("b" -> "s") ::
("c" -> 3) ::
...
2votes
1answer
146views
In scala 2.13, why sometimes TypeTags cannot be inferred? And how to construct one from a variable symbol?
Here is a simple example in shapeless:
it("from Witness") {
val ttg = implicitly[TypeTag[Witness.Lt[String]]]
val ctg = implicitly[ClassTag[Witness.Lt[String]]]
}
it("......
1vote
1answer
88views
In scala 2.13, why is it sometimes impossible to summon type class explicitly? - Part 2
This is a follow up question of In scala 2.13, why is it sometimes impossible to summon type class explicitly?:
The following code can compile properly:
import shapeless._
import record._
import ...
2votes
1answer
87views
In scala 2.13, why is it sometimes impossible to summon type class explicitly?
Here is a simple example in shapeless 2.3.3:
val book =
("author" ->> "Benjamin Pierce") ::
("title" ->> "Types and Programming Languages&...
2votes
1answer
40views
Why does the implicit derivation of this poly function case fails without subtype evidence?
There are two similar shapeless polymorphic functions in the following example. The only difference between them is that deserSucceeding's implicit case definition has an additional subtyping evidence ...
0votes
2answers
58views
How to apply a separate function to each index of a known Shapelss HList
I know the type of an HList and I would like to apply a specific function to each of the specific items in the HList. For example.
I know the type of the HList, in this case String :: Int :: String ::...
1vote
1answer
139views
Scala: missing implicit generic parameter shapeless.LabelledGeneric.Aux[T,L], how to provide it?
I'm writing code for working with DB with finagle-postgres, I have mappers and sure I want to move common parts of code, as much as I can, to generic Mapper.
So, for example, I have Mapper
case class ...
2votes
2answers
77views
How to define an HList type, but based on another HList type
Suppose I have a case class:
case class Foo(num: Int, str: String, bool: Boolean)
Now I also have a simple wrapper:
sealed trait Wrapper[T]
case class Wrapped[T](value: T) extends Wrapper[T]
(and ...
0votes
1answer
179views
Codec for coproducts with discriminator
We're trying to have circe codecs for shapeless coproducts whith a class name as the discriminator :
object CoproductCodecWrong extends App {
import io.circe.Codec
import shapeless.{ :+:, CNil, ...
0votes
1answer
274views
Circe Encoding Sealed Traits and Co Product Case class Fails
I have the following case class:
case class SmartMeterData(
dateInterval: SmartMeterDataInterval = HalfHourInterval(),
powerUnit: PowerUnit = KWH,
smartMeterId: String,
timestamp: String,
...