Java Magazine, Jan/Feb 2017
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2017 49 jvm languages The very same idea is used to translate Scalas moreadvanced features to its object oriented core Code using for such as for x coll println x is shorthand for coll foreach x println x This expansion generalizes to more complicated comprehensions and it applies equally to collection types defined outside the standard library so that it can be used for example to express SQL queries or big data analytics One of my favorite translations is for interpolated strings such as s Hello name It expands to the ordinary method call StringContext Hello s name This provides two hooks to adapt its behavior to your domain you could bring your own StringContext class into scope to override the standard interpolators or you could use Scalas mechanisms for retroactive extension to enrich the existing StringContext with a sql method to support sql SELECT col FROM table Scala aims to be regular and concise with few restrictions or exceptions In a definition all redundant parts are optional meaning the signature of a method may be omitted when it can be inferred and a method is declared abstract simply by omitting its body Public visibility is the default and definitions can be nested arbitrarily a method can be local to another method To allow user defined classes to blend in naturally with primitive types an identifier can be symbolic and punctuation is optional in a method invocation This means that there is no conceptual difference between x y and x y They are just invocations of the method called Although values of primitive type are considered objects they compile to eficient bytecode behind the scenes To further reduce punctuation the semicolon is optional in separating expressions when it can be inferred from a line break Some Examples I think its time to make these ideas more concrete In the examples below what follows the scala prompt is input to the Scala shell launched using the scala command and other lines are feedback from the shell or output from the program I start by defining a class C with a single method message scala class C def message hello defined class C The compiler infers messages result type For a public method its usually a good idea to specify the result type scala class C def message String hello defined class C To harmonize definitions with and without result types a methods result type comes last More generally type ascriptions always follow the entity they describe Of course you shouldnt hardcode the classs message For the common case where a constructor immediately stores each of its arguments in a field for later use a class signature may define a list of arguments prefixing them with the desired keyword for the corresponding member val or var scala class C val message String defined class C scala new C Hi message res1 String Hi In keeping with the mantra that every method invocation targets an object Scala avoids the static keyword and instead offers direct support for the singleton design pattern through object definitions
You must have JavaScript enabled to view digital editions.