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

Issue with this in presence of self alias + & types #99

Merged
merged 7 commits into from
Sep 2, 2022

Conversation

KacperFKorban
Copy link
Collaborator

Continuation of #98

Copy link
Contributor

@OndrejSpanel OndrejSpanel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code seems more readable to me now.

modified.a shouldBe 1
}

// TODO this is an implemenation limitation for now, since anonymous classes crash on runtime
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

limitation of quicklens, Scala 3? If the latter, is there a dotty ticket maybe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

limitation of quicklens, Scala 3?

quicklens. I think.

I'm not sure if there should be a dotty ticket for it since we're using asInstanceOf.
Also, the problem occurs only for the last two tests - the ones with anonymous classes.
It's not hard to minimize, because we can just type the generated code by hand:

sealed trait T
case class A(a: Int) extends T
trait B {
  def a: Int
}

val ab: T & B = new A(0) with B

val res: T & B = // if we change the ascription to `A & B` it works
  if(ab.isInstanceOf[A]) then {
    ab.asInstanceOf[A].copy().asInstanceOf[A & B]
  } else {
    ???
  }

Scastie: https://scastie.scala-lang.org/KacperFKorban/s7A3euoTST2AoPYcTug8iA/5

Maybe there is some other way to write it so that it won't crash on runtime 🤔

The only weird thing is that it doesn't crash if we change the type ascription to the more specific one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: when I rewrite this using with instead of ¨&` and try it with Scala 2.13, it works: https://scastie.scala-lang.org/0vAFV0B8SDmZrjvbRj3jsA

The same code with with still does not work in Scala 3.

To me this looks like a Scala 3 bug.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reported in scala/scala3#15952

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so now that we know it's a quicklens not a dotty issue, can we do anything about it? :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see an immediate solution. The problem is in the generated bytecode, so no matter what we do ab.asInstanceOf[A].copy() will have to be cast to T & B. Which is impossible.

Also if we want to have a discussion about it, maybe a new issue will be a better place for it 😃

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure :) #100

@adamw adamw merged commit 027c55e into softwaremill:master Sep 2, 2022
@adamw
Copy link
Member

adamw commented Sep 2, 2022

Thanks :)

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 this pull request may close these issues.

None yet

3 participants