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

Constructing Writes fail to compile with Scala 3 #1039

Open
levinson opened this issue May 11, 2024 · 1 comment · May be fixed by #1040
Open

Constructing Writes fail to compile with Scala 3 #1039

levinson opened this issue May 11, 2024 · 1 comment · May be fixed by #1040

Comments

@levinson
Copy link

Play JSON Version (2.5.x / etc)

Affected versions:

  • "com.typesafe.play" %% "play-json" % "2.10.5"
  • "org.playframework" %% "play-json" % "3.0.3"

API (Scala / Java / Neither / Both)

Scala

Operating System (Ubuntu 15.10 / MacOS 10.10 / Windows 10)

MacOS 14.3.1

JDK (Oracle 1.8.0_72, OpenJDK 1.8.x, Azul Zing)

Paste the output from java -version at the command line.

java version "17.0.10" 2024-01-16 LTS
Java(TM) SE Runtime Environment (build 17.0.10+11-LTS-240)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.10+11-LTS-240, mixed mode, sharing)

Library Dependencies

None

Expected Behavior

Constructing Writes according to the README should succeed in Scala 3. For example:

import play.api.libs.json._
import play.api.libs.functional.syntax._

case class Location(lat: Double, long: Double)

object ConstructingWrites {
  implicit val locationWrites: Writes[Location] = (
    (JsPath \ "lat").write[Double] and
      (JsPath \ "long").write[Double]
    ) (unlift(Location.unapply))
}

Actual Behavior

The above code from README fails to compile with Scala 3. The compiler gives the following error:

[error] -- [E007] Type Mismatch Error:
[error] 11 |  )(unlift(Location.unapply))
[error]    |           ^^^^^^^^^^^^^^^^
[error]    |           Found:    Location
[error]    |           Required: Option[Any]

As a workaround a custom unapply method can be defined:

import play.api.libs.json._
import play.api.libs.functional.syntax._

case class Location(lat: Double, long: Double)

object ConstructingWrites {
  def locationUnapply(loc: Location): Option[(Double, Double)] = Some((loc.lat, loc.long))

  implicit val locationWrites: Writes[Location] = (
    (JsPath \ "lat").write[Double] and
      (JsPath \ "long").write[Double]
    )(unlift(locationUnapply))
}

Reproducible Test Case

Compile the code from Expected Behavior with Scala 3 in order to reproduce the issue:

scalaVersion := "3.3.3" // works with "2.13.14"

libraryDependencies += "org.playframework" %% "play-json" % "3.0.3" // same behavior with 2.10.5
@levinson levinson linked a pull request May 11, 2024 that will close this issue
5 tasks
@levinson
Copy link
Author

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

Successfully merging a pull request may close this issue.

1 participant