Skip to content

Commit a642443

Browse files
committed
Support Scala 3
This is prompted by guardian/mobile-apps-api#3198 - we would like to be able to upgrade MAPI to Scala 3. As MAPI depends on https://github.com/guardian/cross-platform-navigation, it's good to have a Scala 3 version of this library. The only tricky part of upgrading `cross-platform-navigation` to Scala 3 is handling the explicit call to the `unapply()` method, which _used_ to return an `Option[(...,...,...)]` with all the parameters used to create the case class - in Scala 3 that's changed: * https://docs.scala-lang.org/scala3/guides/migration/incompat-other-changes.html#explicit-call-to-unapply * scala/scala3#2335 The least magical way to cope with the changed `unapply()` method is to manually write out what the unapply method used to produce, and that's what I've done here. Looking at the discussion on the Scala issue, there are other workarounds which are Scala 3-only (and so wouldn't easily work in this project, which cross-compiles with Scala 2): * scala/scala3#2335 (comment) gives us this, which did work for me, but just in Scala 3: ``` def doOldUnapply(ns: NavigationSection) = Some(Tuple.fromProductTyped(ns)) ``` * scala/scala3#2335 (comment) - this is apparently a general version of the method above, but didn't work for me - complained that `NavigationSection` is not a `Product`, I think?
1 parent 4b08abe commit a642443

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

build.sbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import sbtversionpolicy.withsbtrelease.ReleaseVersion
33

44
name:="cross-platform-navigation"
55

6-
ThisBuild / scalaVersion := "2.13.14"
6+
ThisBuild / scalaVersion := "3.3.3"
77

8-
crossScalaVersions := Seq(scalaVersion.value, "2.12.20")
8+
crossScalaVersions := Seq(scalaVersion.value, "2.13.14", "2.12.20")
99

1010
resolvers ++= Resolver.sonatypeOssRepos("releases")
1111

src/main/scala/com/gu/navigation/model/NavigationSection.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ case class NavigationSection(
1515

1616
object NavigationSection {
1717

18+
// def fooUnapply[P <: Product](p: P)(using m: scala.deriving.Mirror.ProductOf[P]): Option[m.MirroredElemTypes] =
19+
// Some(Tuple.fromProductTyped(p))
20+
1821
implicit val navigationSectionReads: Reads[NavigationSection] = (
1922
(__ \ "title").read[String] and
2023
(__ \ "path").read[String] and
@@ -29,14 +32,14 @@ object NavigationSection {
2932
(__ \ "mobileOverride").writeNullable[String] and
3033
(__ \ "sections").lazyWriteNullable(implicitly[Writes[List[NavigationSection]]]) and
3134
(__ \ "editionOverride").writeNullable[String]
32-
)(unlift(NavigationSection.unapply _))
35+
)(unlift((ns: NavigationSection) => Some((ns.title, ns.path, ns.mobileOverride, ns.sections, ns.editionOverride))))
3336

3437
implicit lazy val disNavigationSectionFormat: Format[NavigationSection] = Format(navigationSectionReads, navigationSectionWrites)
3538

3639
}
3740

3841
object Navigation {
39-
implicit val jf = Json.format[Navigation]
42+
implicit val jf: Format[Navigation] = Json.format[Navigation]
4043
}
4144

4245
case class Navigation(pillars: List[NavigationSection])

0 commit comments

Comments
 (0)