Skip to content

Fix #4180: Can't compare Scala and Java Booleans #4181

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

Merged
merged 1 commit into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion library/src/scala/Eq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ object Eq extends Eq[Any, Any] {
implicit def eqNumFloat : Eq[Number, Float] = Eq
implicit def eqDoubleNum: Eq[Double, Number] = Eq
implicit def eqNumDouble: Eq[Number, Double] = Eq
}

implicit def eqSBoolJBool: Eq[Boolean, java.lang.Boolean] = Eq
implicit def eqJBoolSBool: Eq[java.lang.Boolean, Boolean] = Eq
}
4 changes: 2 additions & 2 deletions tests/neg/equality.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ object equality {

1 == true // error

null == true // OK by eqProxy
true == null // error
null == true // OK by eqProxy or eqJBoolSBool
true == null // OK by eqSBoolJBool
null == 1 // OK by eqProxy or eqNumInt
1 == null // OK by eqIntNum

Expand Down
6 changes: 6 additions & 0 deletions tests/pos/i4180.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object Test {
def foo(s: Boolean, j: java.lang.Boolean) = {
val a = s == j
val b = j == s
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Just confirmed those comparisons give the expected result, though I guess this might not warrant a test:

$ /usr/local/bin/dotr
Starting dotty REPL...
scala> val v: java.lang.Boolean = true
val v: Boolean = true
scala> val w: Boolean = true
val w: Boolean = true
scala> v == w
1 |v == w
  |^^^^^^
  |Values of types Boolean and Boolean cannot be compared with == or !=
scala>  implicit def eqSBoolJBool: Eq[Boolean, java.lang.Boolean] = Eq
implicit val eqSBoolJBool: Eq[Boolean, Boolean]
scala> v == w
1 |v == w
  |^^^^^^
  |Values of types Boolean and Boolean cannot be compared with == or !=
scala> w == v
val res0: Boolean = true
scala> w == false
val res1: Boolean = false
scala> implicit def eqJBoolSBool: Eq[java.lang.Boolean, Boolean] = Eq
implicit val eqJBoolSBool: Eq[Boolean, Boolean]
scala> v == w
val res2: Boolean = true
scala> v == false
val res3: Boolean = false