Debunking Scala myths

Viewed 7209

What are the most commonly held misconceptions about the Scala language, and what counter-examples exist to these?

UPDATE

I was thinking more about various claims I've seen, such as "Scala is dynamically typed" and "Scala is a scripting language".

I accept that "Scala is [Simple/Complex]" might be considered a myth, but it's also a viewpoint that's very dependent on context. My personal belief is that it's the very same features that can make Scala appear either simple or complex depending oh who's using them. Ultimately, the language just offers abstractions, and it's the way that these are used that shapes perceptions.

Not only that, but it has a certain tendency to inflame arguments, and I've not yet seen anyone change a strongly-held viewpoint on the topic...

13 Answers

Myth: methods and functions are the same thing.

In fact, a function is a value (an instance of one of the FunctionN classes), while a method is not. Jim McBeath explains the differences in greater detail. The most important practical distinctions are:

  • Only methods can have type parameters
  • Only methods can take implicit arguments
  • Only methods can have named and default parameters
  • When referring to a method, an underscore is often necessary to distinguish method invocation from partial function application (e.g. str.length evaluates to a number, while str.length _ evaluates to a zero-argument function).

Myth: You can replace a fold with a reduce when computing something like a sum from zero.

This is a common mistake/misconception among new users of Scala, particularly those without prior functional programming experience. The following expressions are not equivalent:

seq.foldLeft(0)(_+_)

seq.reduceLeft(_+_)

The two expressions differ in how they handle the empty sequence: the fold produces a valid result (0), while the reduce throws an exception.

Scala has type inference and refinement types (structural types), whereas Java does not.

The myth is busted by James Iry.

Related