Benefit Coproduct over “sealed trait”?

I read the following about coproducts in a great Fearless Guide :

... its value is that Coproducts arent particularly special. The above functionality can be achieved using Either and Nothing instead of: +: and CNil.

Here is the code above:

import shapeless.{Coproduct, :+:, CNil, Inl, Inr}
case class Red()
case class Amber()
case class Green()
type Light = Red :+: Amber :+: Green :+: CNil

val red: Light = Inl(Red())
// red: Light = Inl(Red())
val green: Light = Inr(Inr(Inl(Green())))
// green: Light = Inr(Inr(Inl(Green())))

For my own understanding, what is the use, if any, of using Coproduct over sealed trait?

+4
source share
2 answers

, : ad-hoc-. Coproduct , , , . String Int. ( StringHolder IntHolder case ).

+6

, HList case class: , , . Generic, sealed trait.

+2

Source: https://habr.com/ru/post/1668343/


All Articles