Sensible Defaults

Scala has some defaults that make code concise and easier to read and write. Here are a few of these features:

  • It has support for scripts. Not all code needs to be within a class. If a script is sufficient for your needs, put the executable code directly in a file without the clutter of an unnecessary class.

  • return is optional. The last expression evaluated is automatically returned from method calls, assuming it matches with the return type declared for the method. Not having to put in that explicit return makes code concise, especially when passing closures as method parameters.

  • Semicolons (;) are optional. You don’t have to end each statement or expression with a semicolon—see Semicolon Is Semi-optional—and this reduces noise in the code. To place multiple statements in the same line, use semicolons to separate them. In the absence of a semicolon, Scala will smartly figure out whether a statement or expression is complete, and, if not, will continue to the following line for the rest of the code.

  • Classes and methods are public by default, so you don’t explicitly use the keyword public.

  • Scala provides lightweight syntax to create JavaBeans—it takes less code to create variables and final properties (see Creating Classes).

  • You’re not forced to catch exceptions that you don’t care about—see Exceptions in Scala—which reduces the code size and also avoids improper exception handling.

  • Parentheses and dots are also optional, as we discussed in More Convenience.

In addition, by default Scala imports two packages, the scala.Predef object, and their respective classes and members. You can refer to classes from these preimported packages simply by using their class names. Scala imports the following, in order:

  • java.lang

  • scala

  • scala.Predef

Since java.lang is automatically imported, you can use common Java types in scripts without any imports. So, you can use String, for example, without prefixing it with the package name java.lang or importing it.

You can also use Scala types easily since everything in the package scala is imported.

The Predef object contains types, implicit conversions, and methods that are commonly used in Scala. So, since it is imported by default, you’re able to use those methods and conversions without any prefix or import. They become so convenient that you’ll begin to believe that they are part of the language, when they are actually part of the Scala library.

The object Predef also provides aliases to things like scala.collection.immutable.Set and scala.collection.immutable.Map. So, when you refer to Set or Map, for instance, you’re referring to their definitions in Predef, which in turn refers to their definitions in the scala.collection.immutable package.

Scala’s sensible defaults help make code concise. Next we will see how some of the defaults interplay with operator symbols.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.100.205