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

Lenses can't be created for classes with private constructors for Scala 3 #1426

Open
vadzim-marchanka opened this issue Feb 5, 2024 · 3 comments

Comments

@vadzim-marchanka
Copy link

Hello,

I've noticed that the code that compiles and works fine in scala 2 doesn't work for scala 3.

Scastie:
Scala 2 -> https://scastie.scala-lang.org/FmuLKaM0TpmNvQ3xHdpUSg
Scala 3 -> https://scastie.scala-lang.org/7FBOxnIPQbemh5TomJrUig

Code:

import monocle.syntax.all._

case class A private (field1: String, field2: String)
object A {
  def apply(field1: String, field2: String): A = new A(field1, field2)

  val copiedObject = A("1", "2").copy(field1 = "3")
  val modifiedObject = A("1", "2").focus(_.field1).replace("3")

  println(copiedObject)
  println(modifiedObject)
}

val copiedObject = A("1", "2").copy(field1 = "3") //compile error
val modifiedObject = A("1", "2").focus(_.field1).replace("3") //compile error
println(copiedObject)
println(modifiedObject) 

Error:

missing argument for parameter field2 of method copy in class A: (field1: String, field2: String): Playground.A
method copy cannot be accessed as a member of (from : Playground.A) from module class Playground$.

Could you please clarify whether it is expected behavior?

@julien-truffaut
Copy link
Member

I believe it is expected when you use private in your case class definition.

@vadzim-marchanka
Copy link
Author

Thank you for the response. Should the library behavior be the same for Scala 2 and 3? I expect that people migrating their codebase from Scala 2 to Scala 3 may face this issue.

@julien-truffaut
Copy link
Member

Ideally, yes it should be the same but I believe this is due to a change of semantic between Scala 2 and 3 for private case class constructors. So Monocle is just following the Scala language.

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

2 participants