concept Scala in category lift

appears as: Scala, Scala
Lift in Action: The Simply Functional Web Framework for Scala

This is an excerpt from Manning's book Lift in Action: The Simply Functional Web Framework for Scala.

1.1. What is Scala?

Scala (http://www.scala-lang.org/) is a powerful, hybrid programming language that incorporates many different concepts into an elegant fusion of language features and core libraries. Before delving any deeper, let’s just consider how functional programming differs from imperative programming with languages such as Java and Ruby, and what a functional programming language actually is.

Listing 13.11. Implementing the Author and Book JPA entities with Scala

Appendix A. Introduction to Scala

Scala is a powerful combination of both object oriented and functional programming approaches, and its language constructs can often seem somewhat terse to newcomers. This appendix aims to give you a rough guide to Scala and serve as enough background for you to make full use of this book. Understand that this is only a top-level view of Scala features and it can’t cover anywhere near all of Scala’s capabilities. For a more detailed introduction to the broader language, I suggest picking up a copy of Nilanjan Raychaudhuri’s Scala in Action, also from Manning.

As a language, Scala only has a few core features. The vast majority of features are implemented as libraries, which results in a language that has a very tight core. The following sections cover this core of features that are then used to build the rest of the standard library.

Methods are defined upon classes by using the def keyword, and in this case the resulting type is again inferred by the compiler to be a string. Class methods in Scala don’t need to be explicitly wrapped in curly braces unless the method requires some kind of internal assignment, because all control structures within Scala return a concrete type. Consider the following two method definitions:

def whatever =
  if(bar > 3) "Awesome" else "Amazing"
def another {
  if(bar > 3) "Awesome" else "Amazing"
}

These two method implementations look exceedingly similar, and the unfamiliar eye might assume that they do the same thing. In fact, whatever returns a String whereas another returns a special Scala type called Unit. The reason for this is that the definition of another lacks the equals sign (=) after the method name and immediately wraps the whole method in curly braces; this makes Scala assume that the method is side-effecting and thus returns nothing directly useful. If you’re coming from other statically typed languages, you can think of the Unit type as being analogous to the void construct.

Methods can also take arguments in exactly the same way that Java methods can, but within Scala it’s also possible have multiple argument groups. This can be useful, because it allows you to make flexible APIs that can have both arguments and pseudoblocks. Here’s an example of the definition: