zio-json icon indicating copy to clipboard operation
zio-json copied to clipboard

Scala 3 support

Open jdegoes opened this issue 4 years ago • 9 comments

We should support Scala 3 with the same API as Scala 2. This way users can easily transition from Scala 2 to Scala 3 without having to change any of their ZIO JSON code.

There are several steps to this support:

  1. Enable cross-compilation, which will be easier after #9
  2. Enable type class derivation, this could be done via patching Magnolia or independently via Scala 3 type class derivation

jdegoes avatar Sep 22 '20 11:09 jdegoes

I spent a few hours looking into this. Ignoring tests for now, which are going to have to wait for deps, some import scala.language.implicitConversions are needed (or stop using implicit conversions) and replacing do / while with while according to https://gitter.im/lampepfl/dotty?at=5fbe1088b03a464f082f410d

The efficient replacement for do { body } while (cond) is while ({ body; cond }).

fommil avatar Nov 25 '20 08:11 fommil

Scala 3 in Magnolia is still WIP.

I’m having a branch which uses Scala 3 derivation and implements a subset of the annotations, which I’m now actively working on.

fsvehla avatar Mar 09 '21 21:03 fsvehla

FYI I have a typeclass derivation mechanism that works on Scala 2 and Scala 3. So if we migrated to that we'd get support for both at once.

https://gitlab.com/fommil/shapely

fommil avatar Mar 11 '21 08:03 fommil

The only thing I'm concerned about with shapely is that it appears to use nested eithers / tuples.

But I think it could form the basis for adding derivation directly to ZIO Schema. If we have derivation there, then we can implement ZIO JSON derivation in terms of the Schema.

The limitation right now is that ZIO Schema has no concept of annotations. That would have to be thought out and retrofitted into the existing design.

jdegoes avatar Mar 16 '21 17:03 jdegoes

it appears to use nested eithers / tuples

not sure where you see that. The CaseClassN and SealedTraitN data types are all "flat" so there's one alloc per transformation (and boxing, if folk are using primitives, but nobody's going to be cracking that problem any time soon...)

fommil avatar Mar 18 '21 15:03 fommil

@fommil Probably my mistake, I was trying to "read" the generated code from build.sbt. 😄

My proposal would be to incorporate either @fsvehla's ongoing WIP or Shapley (or parts of both) into ZIO Schema, with added support for annotations, and then base ZIO JSON derivation on that.

jdegoes avatar Mar 18 '21 15:03 jdegoes

I’ll make some time to check out ZIO schema this weekend.

fsvehla avatar Mar 18 '21 18:03 fsvehla

magnolia is supposed to support scala 3 now. apparently.

In terms of what's missing, shapely just needs a little macro that can provide access to the annotations, and a little macro that provides the name of a type, and then it should be able to match the magnolia implementation feature for feature.

fommil avatar Mar 18 '21 22:03 fommil

Isn't this one done?

guizmaii avatar Oct 31 '23 08:10 guizmaii