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

Scala 2.13 - cannot compare child class to parent class in assertTrue #8874

Closed
andrzejressel opened this issue May 18, 2024 · 8 comments · Fixed by #8923
Closed

Scala 2.13 - cannot compare child class to parent class in assertTrue #8874

andrzejressel opened this issue May 18, 2024 · 8 comments · Fixed by #8923

Comments

@andrzejressel
Copy link
Contributor

andrzejressel commented May 18, 2024

Scala 2.13 only

https://scastie.scala-lang.org/rwgljZz2Q6m0aDTwkBDczA

import zio.test.assertTrue

class A
class B extends A
val a = new A()
val b = new B()
assertTrue(a == b)
assertTrue(b == a)

error:

type mismatch;
 found   : Playground.A
 required: Playground.B
not found: value location

Prepared test for SmartAssertionSpec.scala

    test("equalTo compiles when comparing different primitive types") {
      val a = 1
      val b = 1L
      assertTrue(a == b) && assertTrue(b == a)
    },
    test("equalTo compiles when comparing types that are direct descendants") {
      class A
      class B extends A
      val a = new A()
      val b = new B()
      assertTrue(a == b) && assertTrue(b == a)
    },
@andrzejressel
Copy link
Contributor Author

andrzejressel commented May 18, 2024

Now when I think about it - maybe it should work that way? I guess the issue is Scala 3 allowing that ...

I was wrong (from equals Javadoc):

The equals method implements an equivalence relation on non-null object references:

It is symmetric: for any non-null reference values x and y, x. equals(y) should return true if and only if y. equals(x) returns true.

@andrzejressel andrzejressel reopened this May 18, 2024
@andrzejressel andrzejressel changed the title Scala 2.13 - cannot compare child class to parent class Scala 2.13 - cannot compare child class to parent class in assertTrue May 18, 2024
@andrzejressel
Copy link
Contributor Author

andrzejressel commented May 19, 2024

It seems like c.inferImplicitValue does not work for function B => A if they are descendants.

https://docs.scala-lang.org/overviews/quasiquotes/syntax-summary.html#types - tq in ZIO's case return Tree which returns null for .tpe

@kyri-petrou
Copy link
Contributor

kyri-petrou commented May 19, 2024

Hey @andrzejressel, I was going to link this issue on why I think this behaviour is correct for Scala 2 but not for Scala 3, but then I realised you were the one that opened it 😅

Why do you think that in this case Scala 3's behaviour is correct but not Scala 2's?

@andrzejressel
Copy link
Contributor Author

andrzejressel commented May 19, 2024

They are both a bit scuffed - basically I believe that all directly related classes should be comparable in any order.

In Scala 2.13 it didn't work because of bug with implicit conversion finding, in Scala 3 it does not work because this conversion is not implemented for equals (only for > >= < <= comparisons).

EDIT: Even equals JavaDoc agrees with that - comparing parent to child should (must?) return the same as comparing child to parent.

@jdegoes
Copy link
Member

jdegoes commented Jun 5, 2024

/bounty $75

Copy link

algora-pbc bot commented Jun 5, 2024

💎 $75 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #8874 with your implementation plan
  2. Submit work: Create a pull request including /claim #8874 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

Thank you for contributing to zio/zio!

Add a bountyShare on socials

Attempt Started (GMT+0) Solution
🟢 @andrzejressel #8923

Copy link

algora-pbc bot commented Jun 5, 2024

💡 @andrzejressel submitted a pull request that claims the bounty. You can visit your bounty board to reward.

Copy link

algora-pbc bot commented Jun 5, 2024

🎉🎈 @andrzejressel has been awarded $75! 🎈🎊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants