Java Magazine, Jan/Feb 2017
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2017 51 jvm languages scala val y C 1 match case C x x y Int 1 The expression in ys right hand side makes a new object C 1 and immediately picks it apart by comparing it against the pattern C x using pattern matching A pattern can be thought of as a value with holes that are bound to variables so as to extract their values In this case the pattern variable x is assigned to 1 the value passed as Cs first constructor argument and it is then used as the result of the match expression Patterns can be nested to reach directly into the desired part of a data structure In general the compiler ensures that you have covered all possible cases and that all of them are reachable When processing data its often convenient to bundle a few values together in a tuple Because tuples are so common Scala ships with a set of TupleN case classes with N fields for N ranging from 1 to 22 So a tuple behaves just like a regular case class except that its name need not be used when calling its constructor For example Tuple2 a 1 can be abbreviated to a 1 Its useful to think of Unit as Tuple0 with its sole value written as A pattern match with one case such as the one above can be shortened to val C y C 1 to introduce the variable y and bind it to the value of Cs first argument as explained previously This is especially useful when dealing in tuples because it provides a convenient way to return multiple values from a method and immediately name them Pattern matching and tuples in particular help shift coding patterns away from storing intermediate results in mutable local variables and toward computations that can be run efficiently on massive data sets using Apache Spark or Apache Flink for example Conclusion The easiest way to get started with Scala is to install an IDE with Scala support IntelliJ IDEA or Scala IDE for Eclipse These Scala IDEs provide an interactive worksheet the equivalent of a read eval print loop REPL On the command line you may either install a Scala distribution and run the scala command or install the Scala build tool sbt To quickly create an empty Scala project with sbt execute the following at the command prompt sbt new scala scalaseed g8 After launching sbt from the directory created for your new project use the console task to launch the interactive shell and try out the examples in this article Learn More To dig deeper there are some great books on Scala such as Scala for the Impatient Programming Scala and Programming in Scala several free courses on Coursera the first one is Functional Programming Principles in Scala and many more online resources such as Twitters Scala School Have fun article Adriaan Moors @ adriaanm leads the Scala team at Lightbend His first contribution to the Scala compiler in 2007 was support for type constructor polymorphism which was also the topic of his PhD dissertation After a post doc in Martin Oderskys lab at the EPFL he joined Typesafe now Lightbend in 2012 Over the years he has worked on all aspects of the language from the underlying theory the Dependent Object Types calculus and the compiler implementation down to the continuous integration and release infrastructure The easiest way to get started with Scala is to install an IDE with Scala support IntelliJ IDEA or Scala IDE for Eclipse
You must have JavaScript enabled to view digital editions.