Questions tagged [generic-derivation]
33 questions
33
votes
2 answers
How to decode an ADT with circe without disambiguating objects
Suppose I've got an ADT like this:
sealed trait Event
case class Foo(i: Int) extends Event
case class Bar(s: String) extends Event
case class Baz(c: Char) extends Event
case class Qux(values: List[String]) extends Event
The default generic…

Travis Brown
- 138,631
- 12
- 375
- 680
9
votes
1 answer
Generic derivation for ADTs in Scala with a custom representation
I'm paraphrasing a question from the circe Gitter channel here.
Suppose I've got a Scala sealed trait hierarchy (or ADT) like this:
sealed trait Item
case class Cake(flavor: String, height: Int) extends Item
case class Hat(shape: String, material:…

Travis Brown
- 138,631
- 12
- 375
- 680
8
votes
1 answer
Decoding JSON values in circe where the key is not known at compile time
Suppose I've been working with some JSON like this:
{ "id": 123, "name": "aubergine" }
By decoding it into a Scala case class like this:
case class Item(id: Long, name: String)
This works just fine with circe's generic derivation:
scala> import…

Travis Brown
- 138,631
- 12
- 375
- 680
7
votes
1 answer
Encoding ADT case classes with a discriminator, even when typed as the case class
Suppose I have a ADT in Scala:
sealed trait Base
case class Foo(i: Int) extends Base
case class Baz(x: String) extends Base
I want to encode values of this type into the JSON that looks like the following:
{ "Foo": { "i": 10000 }}
{ "Baz": { "x":…

Travis Brown
- 138,631
- 12
- 375
- 680
6
votes
2 answers
Type Class Derivation accessing default values
Is there a clean way to access the default values of a case class fields when performing type class derivation in Scala 3 using Mirrors? For example:
case class Foo(s: String = "bar", i: Int, d: Double = Math.PI)
Mirror.Product.MirroredElemLabels…

Simão Martins
- 1,210
- 11
- 22
5
votes
0 answers
Type Class: Not using `asInstanceOf`, but still getting `ClassCastException`
Consider the following relatively simple code:
import scala.deriving.Mirror
trait TC[A]:
def show: A
object TC:
inline def derived[A](using m: Mirror.ProductOf[A]): TC[A] = new TC[A]:
override def show: A =
val p: Product = new…

Koosha
- 1,492
- 7
- 19
4
votes
1 answer
Transform a case class to another by unwrapping types in Scala 3
I have a enum that represents a container and two case classes:
enum Container[+A]:
case Value(value: A)
case Default(default: A)
def get: A = this match
case Value(value) => value
case Default(default) => default
case class…

ColOfAbRiX
- 1,039
- 1
- 13
- 27
3
votes
1 answer
Scala 3 Method Too large when using Mirror
When I use Mirror of scala 3 to generate a typeclass list, the exception occurs. I know it's the hard limit of jvm of method size, but how can I circumvent this issue.
ps: When delete some fields of Data class it works, but any other…

14sxlin
- 76
- 5
3
votes
1 answer
Blending Magnolia with Circe's trick for automatic derivation
I've got a typeclass and want to provide semi-automatic and automatic derivation to users. I have a working implementation based on Magnolia and it works really well. There's a trait providing definitions for Typeclass[A], combine[A] and…

Regis Kuckaertz
- 991
- 5
- 14
3
votes
1 answer
Clarification on NN residual layer back-prop derivation
I've looked everywhere and can't find anything that explains the actual derivation of backprop for residual layers. Here's my best attempt and where I'm stuck. It is worth mentioning that the derivation that I'm hoping for is from a generic…

Jacob Statnekov
- 245
- 3
- 12
3
votes
2 answers
C# - Call a derived base method in the derived context
Tried to search the web but found nothing so far so here my question:
I want to index model-information via attributes on the different members.
To do this i created a function in a base class that gathers all needed information when called.
This…

Care.Inc
- 31
- 3
2
votes
1 answer
Tuples in Scala 3 Compiler Operations for Typeclass Derivation
Learning some of the new Scala 3 comppiletime operations and a bit confused about Tuple ( particularly using type matching on *: and EmptyTuple)
import scala.compiletime.*
imort cats.Show
transparent inline def showForTuple[T <: Tuple]: Show[T] =
…

Fabián Heredia Montiel
- 1,687
- 1
- 16
- 30
2
votes
1 answer
Cats auto derived with Seq
I want to define equality for some type that can be part of other objects or collections using cats/kitten. I don't want to have to define the equality for every other class.
For example:
import cats.Eq
case class Foo(a: Int, bar: Bar)
case class…

worldwideop
- 51
- 3
2
votes
1 answer
Is it possible to have a sub-trait inheirit a class parameter from another trait?
I am trying to DRY up my code a little bit. I am using Circe to do some decoding. I have several classes and all of them have the form of:
import io.circe.derivation.deriveDecoder
import io.circe.derivation.renaming.snakeCase
import…

CL40
- 579
- 1
- 5
- 12
2
votes
0 answers
Narrow List[T] to a case class with elements of List[A <: T] and Option[B <: T]
I have the following classes;
trait Part
case class Part1() extends Part
case class Part2() extends Part
case class Part3(part1: Part1, part2: Part2) extends Part
case class Part4(part1: Part1, part2: Option[Part2], part3: List[Part3]) extends…

Taha
- 531
- 4
- 21