0

Why can I compare an Int and a String in Scala with ==, like 1=="2", even when this Operator is not defined for a String in the API (http://www.scala-lang.org/api/2.11.8/index.html#scala.Int)?

1
  • Note that the compiler will warn you: "warning: comparing values of types Int and String using `==' will always yield false". There are a number of ways to prevent comparisons of unequal types altogether. For example scalautils.org ; I also wrote a small macro: github.com/Sciss/Equal Commented May 2, 2016 at 23:26

1 Answer 1

3

Because it's defined in Any: def ==(arg0: Any): Boolean

Test two objects for equality. The expression x == that is equivalent to if (x eq null) that eq null else x.equals(that).

Sign up to request clarification or add additional context in comments.

1 Comment

To see this in the docs, you need to click Inherited: "Any" at the top of the scaladoc. For clarity, it's de-selected by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.