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

Comparing Option[T] to T shouldn't be silently compiled #5426

Closed
scabug opened this issue Jan 31, 2012 · 11 comments
Closed

Comparing Option[T] to T shouldn't be silently compiled #5426

scabug opened this issue Jan 31, 2012 · 11 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Jan 31, 2012

Consider such code

val x = Some(5)
val y = 5

println(x==y)

"false" will be printed. Actually, Scala compares those entities in such way that it will be ALWAYS false, so it is indication user didn't want to write the way she/he did (same case when you compare Int to null for example -- you will get at least warning). But here -- nothing, compiler happily compiles the code.

Possible solutions:
a) compiler should evaluate None == Value always to false, and Some(Value) == Value always to true (I prefer such solution)

b) forbid comparing Option[T] to T (compiler error)

b.1) or at least warning

@scabug
Copy link
Author

scabug commented Jan 31, 2012

Imported From: https://issues.scala-lang.org/browse/SI-5426?orig=1
Reporter: Maciej Pilichowski (macias)
Affected Versions: 2.9.1

@scabug
Copy link
Author

scabug commented Jan 31, 2012

@paulp said:
Notice it warns the other way:

scala> println(y == x)
<console>:10: warning: comparing values of types Int and Some[Int] using `==' will always yield false
              println(y == x)
                        ^

a) is not going to happen, equals must be symmetric. b) is not going to happen either. A warning might be possible, but it's harder than it looks to warn where you want to without creating spurious warnings elsewhere.

@scabug
Copy link
Author

scabug commented Jan 31, 2012

@paulp said:
147e9eaf38

@scabug scabug closed this as completed Jan 31, 2012
@scabug
Copy link
Author

scabug commented Jan 31, 2012

Maciej Pilichowski (macias) said:
ad.a) it could be symmetric with no problem

But if 2.10 will fix this, it is OK with me.

@scabug
Copy link
Author

scabug commented Jan 31, 2012

@paulp said:
If Some("a") == "a', how do you propose to modify final class String such that "a" == Some("a") ? If you think equals calls can be intercepted selectively, there is ("a": Object) == (Some("a"): Object). If you think we would rewrite every equals call to achieve this end, there is still a whole universe of java code we don't compile but must interoperate with. It is not no problem. (This is before getting to the fact that even if it were easy and imposed no cost, we wouldn't do it because it isn't true: Some(x) is not x.)

@scabug
Copy link
Author

scabug commented Jan 31, 2012

Maciej Pilichowski (macias) said:
I got idea, you already have equality check, only evaluating to false instead of true (after all it has to be defined somewhere, right?). I wouldn't worry about Java, because the problem is in Scala, Java has different rules and "A op B" can have different meaning in Java and Scala. "Some is not x." -- depends on perception, I am not saying it IS x, I am saying it is equal to x. "0.9(9)" IS not 1, but it is equal to 1. You can think of Option as -- solid value or nothing (None). Some(X) is representation of solid value. And besides what benefit is with evaluation to false? None, you have to make the code longer -- and Scala was supposed to cut unnecessary code. So you have to write "if (x.isDefined && x.get==y) ..." instead of just "if (x==y) ..."

@scabug
Copy link
Author

scabug commented Jan 31, 2012

@retronym said:
Best to drift this over to the mailing list [scala-debate]; here isn't the right forum. BTW, if (x == Some\(y)) ... ain't so bad..

@scabug
Copy link
Author

scabug commented Jul 11, 2012

@SethTisue said (edited on Jul 11, 2012 10:07:18 PM UTC):
re x == Some(y), see #6064

@scabug scabug added this to the 2.10.0 milestone Apr 7, 2017
@n0tel
Copy link

n0tel commented Jun 13, 2017

val seq = Seq(Some(5))
seq.find(_ == 5)

Do not warn if the comparison is within a Seq

@avbhandaru
Copy link

Is there a compiler flag for this?

@lrytz
Copy link
Member

lrytz commented Feb 15, 2024

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

5 participants