Scala Programming Language
  1. Scala Programming Language
  2. SI-5426

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

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: Scala 2.9.1
    • Fix Version/s: Scala 2.10.0
    • Component/s: Misc Compiler
    • Labels:
      None
    • Environment:

      openSUSE/Linux

      Description

      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

        Activity

        Hide
        Paul Phillips added a comment -

        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.

        Show
        Paul Phillips added a comment - 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.
        Hide
        Paul Phillips added a comment -

        147e9eaf38

        Show
        Paul Phillips added a comment - 147e9eaf38
        Hide
        Maciej Pilichowski added a comment -

        ad.a) it could be symmetric with no problem

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

        Show
        Maciej Pilichowski added a comment - ad.a) it could be symmetric with no problem But if 2.10 will fix this, it is OK with me.
        Hide
        Paul Phillips added a comment -

        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 is not x.)

        Show
        Paul Phillips added a comment - 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 is not x.)
        Hide
        Maciej Pilichowski added a comment -

        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) ..."

        Show
        Maciej Pilichowski added a comment - 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) ..."
        Hide
        Jason Zaugg added a comment -

        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..

        Show
        Jason Zaugg added a comment - 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..
        Hide
        Seth Tisue added a comment - - edited

        re x == Some(y), see SI-6064

        Show
        Seth Tisue added a comment - - edited re x == Some(y), see SI-6064

          People

          • Assignee:
            Paul Phillips
            Reporter:
            Maciej Pilichowski
          • Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development