Skip to content
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

Mononcle doesn't support @Lenses in Scala3. #1337

Open
dev-jiwonyang opened this issue Jan 31, 2023 · 10 comments
Open

Mononcle doesn't support @Lenses in Scala3. #1337

dev-jiwonyang opened this issue Jan 31, 2023 · 10 comments
Assignees
Labels
Milestone

Comments

@dev-jiwonyang
Copy link

dev-jiwonyang commented Jan 31, 2023

I used @Lenses macro annotation in Scala2, but I can't find @Lenses in Monocle 3.1.0 with Scala3.
Does anyone know anything about this issue?
Does Monocle have any plan to support @Lenses in Scala3?

@cquiroz
Copy link
Collaborator

cquiroz commented Mar 11, 2023

Scala 3 doesn't support (yet) macro annotations such as @Lenses. Until such support is provided it won't be possible to support @Lenses

@armanbilge
Copy link
Contributor

@nkgm
Copy link

nkgm commented Apr 2, 2023

I am no expert, but following these PR's I'm under the impression this is still impossible under Scala3 post-typer Macro Annotations. I'm sure there will be other similar opportunities under the new Scala3 metaprogramming facilities, but in any case, Macro Annotations have been merged since 3.3.0-RC2 so hopefully we'll have a definitive answer soon.

@nafg
Copy link

nafg commented Jun 9, 2023

@nkgm correct. I think a more fruitful approach would actually be to lobby for inclusion in Scala proper. It doesn't mean including an optics library, just the following:

A. A case class or something basically of the form case class Field[A, B](get: A => B, set: B => A => A). Although I would like for it to also include name: String.
B. Some syntax (e.g. a macro annotation or something else) that gets the compiler to generate a bunch of these for you

A macro that generates a single one would be very easy to write, so there's no need for them to special-case that.

Meanwhile, the recommended migration path is to use the focus macros instead. Although, I wonder if there's a performance effect, since lenses as fields would reuse the same instance.

@yilinwei
Copy link
Collaborator

I think it's been enough time that we need to start looking into alternatives for folks to migrate.

Off the top of my head I know that simulacrum looked into using scalafix rules to do this.

@nafg
Copy link

nafg commented Dec 21, 2023

Maybe some way with named tuples?

Or like I said before, just start a pre-SIP.

Code gen seems dubious since it belongs in a companion. Unless I make the companions extend a trait that is generated.

The quick fix approach that was discussed on the forums might be interesting though.

@yilinwei
Copy link
Collaborator

yilinwei commented Jan 2, 2024

I was hoping to do the rewrite pre-compliation so it wouldn't be in your source tree; for all intents and purposes it would just allow the same source to be used for Scala 2/3.

What quick fix approach were you referencing?

@nafg
Copy link

nafg commented Jan 2, 2024

@nafg
Copy link

nafg commented Jan 2, 2024

How can code not in the source tree be part of the companion object of code that is

@yilinwei
Copy link
Collaborator

yilinwei commented Jan 2, 2024

Some build tools (dune from the OCaml ecosystem iirc) distinguish the source tree to the sources compiled by the compiler - the first step is to symlink the files into a target directory before compilation and run the compiler on those sources. Generated sources are also written directly to this directory and the compiler essentially runs on this folder.

In this build model, it's quite trivial to rewrite the sources before the compile step - I'm not entirely sure whether sbt or mill follows the same model. If it didn't, we could do something a little bit more involved where we filter the sources which use the trait and instead dump an edited source in the managedSources/generatedSources.

The idea would be to look for the @Lens marker trait and rewrite an existing companion object to add some extra lines of code to synthesize the required companion objects. That way, the semantics are the same for Scala 3/Scala 2 and it will ease migration until a better alternative emerges.

@yilinwei yilinwei self-assigned this Jan 3, 2024
@yilinwei yilinwei added this to the 3.3 milestone Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants