Before we dive into Scala’s support for object-oriented and functional programming, let’s finish our discussion of the essential features you’ll use in most of your programs.
An important fundamental concept in Scala is that all operators are actually methods. Consider this most basic of examples:
That plus sign between the
numbers? It’s a method. First, Scala allows non-alphanumeric method names. You can call
or whatever you desire. Second, this expression is identical to
.+(2). (We put a space after the
1. would be interpreted as a
Double.) When a method takes one argument, Scala lets
you drop both the period and the parentheses, so the method invocation
looks like an operator invocation. This is called “infix” notation, where
the operator is between the instance and the argument. We’ll find out more
about this shortly.
Similarly, a method with no arguments can be invoked without the period. This is called “postfix” notation.
Ruby and Smalltalk programmers should now feel right at home. As users of those languages know, these simple rules have far-reaching benefits when it comes to creating programs that flow naturally and elegantly.
So, what characters can you use in identifiers? Here is a summary of the rules for identifiers, used for method and type names, variables, etc. For the precise details, see [ScalaSpec2009]. Scala allows all the ...