Why Scala instead of Java?

Isn’t the equivalent of java’s record in Scala the case class, which is simply
case class MyClass(index: Int, name: String)?

That defines equals, hashcode, canEqual, and a whole lot more. In any case, case classes have data equality rather than instance equality, which is what the redefinition in your code does.

The useful equivalent of a regular Java class in Scala would rather be
class MyClass(var index: Int, var name: String) if you wanted mutable fields, or class MyClass(val index: Int, val name: String) if you wanted immutable but outwardly accessible fields.

In both cases, Scala’s more succinct, and it’s that way without a need to introduce the baggage that’s associated with record or case class.

5 Likes

Thank you for the correction! I’ve tried to look but I can’t find any documentation about the meaning of the keyword “case” when used in class context. Was the word “case” chosen for this use case because these case classes can be used in match statements?

data match {
  case Document("readme.txt") => ...
  ...
}

Potentially. You’d have to ask someone who’s been using Scala longer than I have. In any case, that’s one of the additional methods that case class defines beyond what record in Java does. The case keyword before class causes the unapply and apply methods on the companion object of the case class to be defined, as well as the ones you already listed.

Also, I’d like to address this:

The Java example that most closest matches that Scala class would be this quite useless piece of code:

public class MyClass {
    public MyClass(int index, String name) {
    }
}

While you’re correct that your example is probably the near equivalent of what class MyClass(index: Int, name: String) would produce in the bytecode, it’s not a very helpful example. A lot of boilerplate from Java stops being necessary due to the lack of utility of the class. Take this slightly adjusted example:

class MyClass(index: Int, name: String):
  def character: Char = name.charAt(index)

The equivalent to this in java would be:

public class MyClass {
  private final int index; 
  private final String name;

  public MyClass(int index, String name) {
    this.index = index;
    this.name = name;
  }
  
  public char character() {
    return name.charAt(index);
  }
}

As soon as we introduce any utility to the class definition, the verbosity of the Java definition of the class blossoms, while Scala remains rather concise.

2 Likes

Originally, I believe so, way back in the mists of time. But over the years, it’s become its own distinct jargon, with that as just one of many features: case class is simply the standard way to define an immutable data structure.

In idiomatic Scala code, it’s quite common for most/all data structures to be defined as case classes, since they’ve evolved to nicely suit the typical use cases with a minimum of boilerplate. In particular, the synthetic copy() method makes it relatively easy to build data structures that are both immutable and efficient.

2 Likes