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

More typesafe >, <, = comparisons #791

Merged
merged 3 commits into from
Dec 15, 2022

Conversation

sviezypan
Copy link
Collaborator

The bug was that compiler did not complain when user compared 2 values of different types:

select(customerId, fName, lName, verified, dob).from(customers).where(fName === true)
//or
select(customerId, fName, lName, verified, dob).from(customers).where(verified === "hello")

The problem was that in Expr where === is defined

    def ===[F2, A1 <: A, B2 >: B](that: Expr[F2, A1, B2]): Expr[F :||: F2, A1, Boolean] =
      Expr.Relational(self, that, RelationalOp.Equals)

When B =:= Boolean and B2 =:= String, Scala happily infers B2 to be Any, so that would compile.

This PR introduces ComparableTypes trait which would prevent that behaviour.
We still allow to compare different numeric or date types like 3 < 5.5 etc.

@sviezypan sviezypan requested a review from a team as a code owner December 11, 2022 16:10

@implicitNotFound(
"You cannot compare values of different types ${A} and ${B}. " +
"As those are unrelated types, this query would blow up at database level."
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"As those are unrelated types, this query would blow up at database level."
"As those are unrelated types, this query would fail at database level."

@sviezypan sviezypan merged commit 78f7e70 into zio:master Dec 15, 2022
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.

2 participants