1

For example:

scala> val a: String = "5"
a: String = 5

scala> val b: Int = 5
b: Int = 5

scala> a == b
res5: Boolean = false

I expect a TypeError, like in Java:

class MyClass {
    public static void main(String[ ] args) {
        int a = 5;
        String b = "5";
        System.out.println(a == b);
    }
}

I wonder what was the reasoning of scala designers to not doing type checking. I personally don't see any advantage of not doing it. When could it be practically useful? However, it could lead to run time bugs.

Also, I feel, that stronger type checking is more inline with functional paradigm, why did scala changed that behavior of its predecessor java?

7

Scala inherited this behavior from Java.

The key is that Scala's == is equivalent to Java's equals, not == (which checks referential equality).

  • Is the following statement correct? : Scala inherited equals from Java; equals in Scala act the same as Java. But Scala designers decided to use == to call equals instead of doing using == for referential equality. – Akavall May 2 '17 at 18:19
  • @Akavall yes, that's essentially correct – Morgen May 2 '17 at 18:33
2

Scala inherited the concept of universal equality from its host languages. Remember, Scala is designed to be a hosted language, i.e. a language that becomes part of another language's ecosystem. For example, the (now abandoned) Scala.NET on the .NET platform, Scala-JVM on the Java platform, Scala.js on the ECMAScript platform, and so on.

What all of those languages have in common, is universal equality:

// ### Java:
String s = "42";
int    i =  42;


// ### Value equality:
s.equals(i);
//=> false


// ### Reference equality:
s == i
// error: bad operand types for binary operator '=='
// ### C♯:
var s = "42";
var i =  42;

// ### Value equality:
s.Equals(i);
//=> False

s == i;
// error CS0019: Operator `==' cannot be applied to operands of type `string' and `int'


// ### Reference equality:
object.ReferenceEquals(s, i);
//=> False
// ### ECMAScript:
const s = "42";
const i =  42;

// ### Value equality:
s == i;
//=> true

s === i;
//=> false
// ### Scala:
val s = "42"
val i =  42

// ### Value equality:
s == i
//=> false


// ### Reference equality:
s eq i
// error: the result type of an implicit conversion must be more specific than AnyRef

As you can see, Scala is not alone in returning a boolean for value equality. Java, C♯ (except when using the == operator), and ECMAScript behave similarly. (ECMAScript even returns true in one case!)

There are, however, people in the Scala community who think hard about equality. Thankfully, they don't only complain, they also offer solutions, for example, Scalaz's Equal typeclass and cats' Eq typeclass are not universal.

  • Is the following statement correct? : Scala inherited equals from Java; equals in Scala act the same as Java. But Scala designers decided to use == to call equals instead of doing using == for referential equality. – Akavall May 2 '17 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.