Questions tagged [coproduct]

10 questions
15
votes
1 answer

My coproduct encoding is ambiguous

This question has come up a few times recently, so I'm FAQ-ing it here. Suppose I've got some case classes like this: import io.circe._, io.circe.generic.semiauto._ object model { case class A(a: String) case class B(a: String, i: Int) case…
Travis Brown
  • 135,682
  • 12
  • 352
  • 654
4
votes
2 answers

Existential type classes vs. Data Constructors vs. Coproducts

I find myself running up against the same pattern in my designs where I start with a type with a few data constructors, eventually want to be able to type against those data constructors and thus split them into their own types, just to then have to…
3
votes
1 answer

GADT Type as Shapeless Coproduct -- how to build an Interpreter with an arbitrary number of Algebras

Let say I have two GADT types. abstract class Numbers[A]() case class IntType() extends Numbers[Int] abstract class Letters[A]() case class EnglishType() extends Letters[String] And I have an Interpreter for each of the GADT types which…
Travis Stevens
  • 2,038
  • 2
  • 17
  • 24
3
votes
1 answer

How to make only few datatype which is not related to each other acceptable by generics

There is a trait which works perfectly. However, I would like to refactor the part related to generic [T] in order to limit the data type which could be accepted by generic [T] (I need only Option[JsValue] , JsValue , StringEnumEntry , String ). Is…
Tim Florian
  • 279
  • 1
  • 2
  • 12
3
votes
0 answers

How do I define value of discriminator for coproduct codecs?

Could you please explain how coproducts really work? Here is my code sealed trait ArdbData case class ArdbDataString(value: String) extends ArdbData case class ArdbDataLong(value: Long) extends ArdbData case class ArdbDataDouble(value: Double)…
expert
  • 26,897
  • 27
  • 105
  • 198
2
votes
0 answers

Prepend more than two coproducts in Shapeless

I'd like to prepend more than two separate Coproducts into one: import shapeless.syntax._ case class Aa(name: String) case class Bb(age: Int) case class Cc(ok: Boolean) case class Dd(x: Long) case class Ee(y: Double) case class Ff(z:…
KaC
  • 501
  • 5
  • 13
2
votes
0 answers

Library for pattern matching shapeless coproduct with partial functions

I have a relatively ergonomic pattern I use to pattern match shapeless Coproducts. It looks basically like this val stringOrInt: String :+: Int :+: CNil = Coproduct("Hello world") implicit class Coproduct2Syntax[A, B](val value: A :+: B :+: CNil)…
kag0
  • 4,254
  • 5
  • 30
  • 55
2
votes
1 answer

Shapeless: Iterate over the types in a Coproduct

I want to do something really simple, but I'm struggling to craft the correct search or just understanding some of the solutions I've seen. Given a method that takes a generic type parameter which is a Coproduct; def apply[T <: Coproduct] = { …
Ryan Worsley
  • 645
  • 1
  • 4
  • 16
0
votes
1 answer

Coproducts in shapeless does not compile

I'm experimenting with shapeless and now trying to understand Generic for Coproducts. Here is what I tried: object ShapelessExperiments { final case class Test1() final case class Test2() final case class Test3() type Test = Test1…
Some Name
  • 6,872
  • 4
  • 9
  • 32
0
votes
1 answer

Get a specific implementation of a parametrized trait from a parametrized function

Trying to get a parametrized function that would return the specified coproduct of a trait. sealed trait Tr { def x: Int } case class Cl1(x: Int) extends Tr case class Cl2(x: Int) extends Tr def getTr[A <: Tr](i: Int): A = { ??? } How can this…
Vasily802
  • 1,314
  • 16
  • 28