Java Magazine, Jan/Feb 2017
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2017 48 jvm languages Functional thinking and design are ingrained in Scalas DNA from the choice of keywords val for immutable and var for mutable variables having type ascriptions follow rather than precede the variable name so they can be omitted easily thanks to type inference and pattern matching deeply integrated into the object oriented core to preferring definition site variance over wildcards the latter tends to clutter users code In Scala a function is a small unit of abstraction well suited to capture a single computation where the result is solely determined by a few inputs A function is easy to understand in isolation which also means it can more readily be distributed across many cores or machines In FP functions play the role that objects have in object oriented programming they are first class values and the key building blocks that determine a programs behavior Objects and classes represent larger abstractions that encapsulate the interplay of many diferent members Where a composition of functions expresses an algorithm a composition of objects models a system Scala unifies the safety and performance of static type checking with the ease of use of a scripting language through type inference Because of type inference most type annotations can be omitted when expedient Types can often be omitted when they impede readability where they are a barrier to potential refactorings to the rapid development of a prototype or to the exploration of a new problem in Scalas interactive shell or a data science worksheet Many programmers think of FP as disavowing mutable state or more generally side effects requiring lazy evaluation or deep mathematical reasoning This is not how functional programming is interpreted in Scala Eventually code must have an effect on the outside world whether that code is functional or object oriented However unneeded or unexpected side effects usually adversely afect maintainability and scalability in both functional and object oriented programming Scala gives you a choice between pure programming which favors immutable variables and recursion and more imperative programming which uses mutable variables and while loops However Scala does try to nudge you away from side effects There is no separate notion of a statement because it is considered an expression that computes an uninteresting result An expression of type Unit the equivalent of void is just another type which is integrated with generics It just happens to have only one value the uninteresting result which is usually discarded This unification of expressions and statements simplifies a few things functions need not distinguish whether their body is a statement that is returning void or an expression Ultimately Scala is a purely object oriented language This means that conceptually every value in Scala is an object and every operation is a method call Other highlevel conveniences of the language such as functions are translated into this core by expanding them to method calls behind the scenes This capability makes it easy to write domain specific languages DSLs in Scala The use of such DSLs has been implemented successfully by Apache Spark for data science Slick for database queries and the sbt build tool for declarative build definitions One aspect of Scalas deep integration of FP is simply to translate a function application f x to the method call f apply x A function is modeled as an object with an apply method and any object that has this method is thus eligible for this convenience Scala aims to be regular and concise with few restrictions or exceptions In a definition all redundant parts are optional
You must have JavaScript enabled to view digital editions.