8000 Support deriving Encoder / Decoder for `NamedTuples` ? · Issue #2326 · circe/circe · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support deriving Encoder / Decoder for NamedTuples ? #2326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Quafadas opened this issue Nov 8, 2024 · 3 comments
Open

Support deriving Encoder / Decoder for NamedTuples ? #2326

Quafadas opened this issue Nov 8, 2024 · 3 comments

Comments

@Quafadas
Copy link
Quafadas commented Nov 8, 2024

Here's the (trivial) example...

  val nt: (name: String, age: Int) = (name = "Simon", age = 40)

  given fooDecoder1: Decoder[Foo] =
    deriveDecoder[(name: String, age: Int)]

and it's error

[{
	"resource": "/Users/simon/Code/namedTupleTest/nt.scala",
	"owner": "_generated_diagnostic_collection_name_#3",
	"code": "172",
	"severity": 8,
	"message": "No given instance of type scala.deriving.Mirror.Of[(name : String, age : Int)] was found for parameter A of method deriveDecoder in object semiauto. Failed to synthesize an instance of type scala.deriving.Mirror.Of[(name : String, age : Int)]:\n\t* class Any is not a generic product because it is not a case class\n\t* class Any is not a generic sum because it is not a sealed class",
	"source": "bloop",
	"startLineNumber": 31,
	"startColumn": 44,
	"endLineNumber": 31,
	"endColumn": 44
}]

With I guess the (slightly tedious?) workaround;

  given fooDecoder1: Decoder[(name: String, age: Int)] =
    new Decoder[(name: String, age: Int)] {
      def apply(c: HCursor): Decoder.Result[(name: String, age: Int)] =
        for {
          name <- c.downField("name").as[String]
          age <- c.downField("age").as[Int]
        } yield (name, age)
    }

  given fooEncoder1: Encoder[(name: String, age: Int)] =
    new Encoder[(name: String, age: Int)] {
      def apply(a: (name: String, age: Int)): Json =
        Json.obj(
          "name" -> Json.fromString(a.name),
          "age" -> Json.fromInt(a.age)
        )
    }

@Quafadas Quafadas changed the title Support NamedTuples ? Support deriving Encoder / Decoder for NamedTuples ? Nov 8, 2024
@jilen
Copy link
jilen commented Dec 19, 2024

There is a generic implementaion
https://scastie.scala-lang.org/JLAz8i1CTaqtvyblPvlOEA

@MartinHH
Copy link
Contributor
MartinHH commented Mar 7, 2025

Just pointing out the more more or less obvious here: the general problem with library support for named tuples is: library authors are encouraged to publish for LTS scala versions (currently: 3.3.x) "unless your library is built around some new language feature available only on Next". So to add such support, there would be three options:

  1. wait until a new LTS version (>3.6.x) is out
  2. do not follow the official recommendations and up the scala version to 3.6.x (seems like a bad idea)
  3. publish the specific feature in a deficated 3.6.x-module while the rest of the library remains on 3.3.x

@mzuehlke
Copy link
Contributor
mzuehlke commented Mar 9, 2025

The next LTS will likely be release in Q4 2025.
So no too far in the future.

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

No branches or pull requests

4 participants
0