Skip to content

Scala 3. Derivation for enums. Better derivation for sealed traits #283

Merged
merged 3 commits into from
May 21, 2023

Conversation

susliko
Copy link
Contributor

@susliko susliko commented May 16, 2023

Addresses #199, #281

Derivation for enums

Codecs are now derived for Scala 3 enums, both simple and parametrized.
All Phobos annotations and ElementCodecConfig are respected
E.g., the following declaration is now valid:

enum Domestic derives ElementEncoder, ElementDecoder:
  @discriminator("tiger") case Cat
  @discriminator("wolf") case Dog(@text breed: String)

Derivation for sealed traits

Up to this moment sealed trait derivation required explicit derivation of instances for its children:

sealed trait Wild derives ElementEncoder, ElementDecoder
object Wild {
  @discriminator("cat") case object Tiger                    extends Wild derives ElementEncoder, ElementDecoder
  @discriminator("dog") case class Wolf(@text breed: String) extends Wild derives ElementEncoder, ElementDecoder
}

For the sake of ergonomics and consistency with enum derivation, the following declaration is now valid:

sealed trait Wild derives ElementEncoder, ElementDecoder
object Wild {
  @discriminator("cat") case object Tiger                    extends Wild
  @discriminator("dog") case class Wolf(@text breed: String) extends Wild

Instances for case object children of sealed traits are also derived now

Side changes

  • fixed typo in the EncoderError message
  • formatted Scala 3 files, updated .scalafmt.conf

@valentiay valentiay added this to the v0.21.0 milestone May 21, 2023
Copy link
Contributor

@valentiay valentiay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Thanks for your contribution. I'm glad to see, that enum derivation is now supported. The PR looks good to me. Going to merge and publish it in 0.21.0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants