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

Cannot use modify in opaque types #234

Open
OndrejSpanel opened this issue Jun 30, 2024 · 2 comments
Open

Cannot use modify in opaque types #234

OndrejSpanel opened this issue Jun 30, 2024 · 2 comments
Assignees

Comments

@OndrejSpanel
Copy link
Contributor

Following opaque type has defined both getters and copy, but it is not possible to use quicklens on it:

import com.softwaremill.quicklens.*

object Types {
  case class V(x: Double, y: Double)

  opaque type Vec = V

  object Vec {
    def apply(x: Double, y: Double): Vec = V(x, y)
  }
  extension (v: Vec) {
    def x: Double = v.x
    def y: Double = v.y
    def copy(x: Double = v.x, y: Double = v.y): Vec = V(x, y)
  }
}

object Main {
  import Types.*
  def main(args: Array[String]): Unit = {
    val a = Vec(1, 2)
    val b = a.modify(_.x).using(_ + 1)
    println(b)
  }

}

With Scala 3.4.2 the error is:

Unsupported path element. Path must have shape: .field1.field2.each.field3.(...), got: (($1: Types.Vec) => Types.x($1))
val b = a.modify(
.x).using(_ + 1)

@OndrejSpanel
Copy link
Contributor Author

It is not only opaque types. Following wrapper gives the same error:

import com.softwaremill.quicklens.*

object Types {
  case class V(x: Double, y: Double)

  class Vec(val v: V)

  object Vec {
    def apply(x: Double, y: Double): Vec = new Vec(V(x, y))
  }
  extension (v: Vec) {
    def x: Double = v.v.x
    def y: Double = v.v.y
    def copy(x: Double = v.x, y: Double = v.y): Vec = new Vec(V(x, y))
  }
}

object Main {
  import Types.*
  def main(args: Array[String]): Unit = {
    val a = Vec(1, 2)
    val b = a.modify(_.x).using(_ + 1)
    println(b)
  }

}

@OndrejSpanel
Copy link
Contributor Author

OndrejSpanel commented Jun 30, 2024

And when I remove extension and use members instead, the code works in Scala 2.13.14, but not in Scala 3:

import com.softwaremill.quicklens._

object Types {
  case class V(x: Double, y: Double)

  class Vec(val v: V) {
    def x: Double = v.x
    def y: Double = v.y
    def copy(x: Double = v.x, y: Double = v.y): Vec = new Vec(V(x, y)) {}
  }

  object Vec {
    def apply(x: Double, y: Double): Vec = new Vec(V(x, y)) {}
  }
}

object Main {
  import Types._
  def main(args: Array[String]): Unit = {
    val a = Vec(1, 2)
    val b = a.modify(_.x).using(_ + 1)
    println(b)
  }

}

The error in Scala 3 is:

Unsupported source object: must be a case class or sealed trait, but got: val
val b = a.modify(.x).using( + 1)

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