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

Currencies don't compare correctly when defined inside a trait #402

Open
txsmith opened this issue Jun 23, 2020 · 0 comments
Open

Currencies don't compare correctly when defined inside a trait #402

txsmith opened this issue Jun 23, 2020 · 0 comments

Comments

@txsmith
Copy link

txsmith commented Jun 23, 2020

Hello! We've hit some problems in our code when defining a currency in one of our base traits:

trait ConvenientStuffWeUseEverywhere {
  object OurCurrency extends squants.market.Currency(...)
}

class ServiceA extends ConvenientStuffWeUseEverywhere {
  val someMoney = Money(100, OurCurrency)
}
class ServiceB extends ConvenientStuffWeUseEverywhere {
  val otherMoney = Money(100, OurCurrency)
}

Interestingly because OurCurrency is defined in a trait that is extended twice, ServiceA and ServiceB both end up with a freshly created instance of OurCurrency. This causes ServiceA.someMoney == ServiceB.otherMoney to be false in our tests!

We've fixed our specific case by overriding equals and hashcode in OurCurrency, but it might be something that should be Currency to avoid having to override it for every currency you define.

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

No branches or pull requests

1 participant