Scala Language

Topics related to Scala Language:

Getting started with Scala Language

Functions

Scala has first-class functions.

Difference between functions and methods:

A function is not a method in Scala: functions are a value, and may be assigned as such. Methods (created using def), on the other hand, must belong to a class, trait or object.

  • Functions are compiled to a class extending a trait (such as Function1) at compile-time, and are instantiated to a value at runtime. Methods, on the other hand, are members of their class, trait or object, and do not exist outside of that.
  • A method may be converted to a function, but a function cannot be converted to a method.
  • Methods can have type parameterization, whereas functions do not.
  • Methods can have parameter default values, whereas functions can not.

While Loops

The primary difference between while and do-while loops is whether they execute the block_expression before they check to see if they should loop.

Because while and do-while loops rely on an expression to evaluate to false to terminate, they often require mutable state to be declared outside the loop and then modified inside the loop.

Pattern Matching

For Expressions

Collections

Type Parameterization (Generics)

Error Handling

Extractors

Case Classes

Traits

XML Handling

Enumerations

String Interpolation

This feature exists in Scala 2.10.0 and above.

Currying

Partial Functions

Higher Order Function

Scala goes to great lengths to treat methods and functions as syntactically identical. But under the hood, they are distinct concepts.

A method is executable code, and has no value representation.

A function is an actual object instance of type Function1 (or a similar type of another arity). Its code is contained in its apply method. Effectively, it simply acts as a value that can be passed around.

Incidentally, the ability to treat functions as values is exactly what is meant by a language having support for higher-order functions. Function instances are Scala's approach to implementing this feature.

An actual higher-order function is a function that either takes a function value as an argument or returns a function value. But in Scala, as all operations are methods, it's more general to think of methods that receive or return function parameters. So map, as defined on Seq might be thought of as a "higher-order function" due to its parameter being a function, but it is not literally a function; it is a method.

Type Variance

Implicits

Implicit classes allow custom methods to be added to existing types, without having to modify their code, thereby enriching types without needing control of the code.

Using implicit types to enrich an existing class is often referred to as an 'enrich my library' pattern.

Restrictions on Implicit Classes

  1. Implicit classes may only exist within another class, object, or trait.
  2. Implicit classes may only have one non-implicit primary constructor parameter.
  3. There may not be another object, class, trait, or class member definition within the same scope that has the same name as the implicit class.

Classes and Objects

Operator Overloading

Option Class

JSON

Java Interoperability

Regular Expressions

Setting up Scala

Var, Val, and Def

Futures

Working With Gradle

synchronized

Single Abstract Method Types (SAM Types)

Single Abstract Methods are types, introduced in Java 8, that have exactly one abstract member.

Streams

Streams are lazily-evaluated, meaning they can be used to implement generators, which will provide or 'generate' a new item of the specified type on-demand, rather than before the fact. This ensures only the computations necessary are done.

Parser Combinators

ParseResult Cases

A ParseResult comes in three flavors:

  • Success, with a marker as to the start of the match and the next character to be matched.
  • Failure, with a marker as to the start of where the match was attempted. In this case the parser backtracks to that position, where it will be when parsing continues.
  • Error, which stops the parsing. No backtracking or further parsing occurs.

Type-level Programming

Annotations

Macros

Macros are a language feature that need to be enabled, either by importing scala.language.macros or with the compiler option -language:macros. Only macro definitions require this; code that uses macros need not do it.

Type Classes

To avoid serialization problems, particularly in distributed environments (e.g. Apache Spark), it is a best practice to implement the Serializable trait for type class instances.

Parallel Collections

Recursion

Quasiquotes

Monads

If Expressions

Best Practices

Scaladoc

Self types

Often used with the cake pattern.

Type Inference

Tuples

Why are tuples limited to length 23?

Tuples are rewritten as objects by the compiler. The compiler has access to Tuple1 through Tuple22. This arbitrary limit was decided by language designers.

Why do tuple lengths count from 0?

A Tuple0 is equivalent to a Unit.

Testing with ScalaTest

Reflection

Dependency Injection

Handling units (measures)

It is recommended to use value classes for units or a dedicated library for them.

Working with data in immutable style

Value and variable names should be in lower camel case

Constant names should be in upper camel case. That is, if the member is final, immutable and it belongs to a package object or an object, it may be considered a constant

Method, Value and variable names should be in lower camel case

Source: http://docs.scala-lang.org/style/naming-conventions.html

This compile:

val (a,b) = (1,2)
// a: Int = 1
// b: Int = 2

but this doesn't:

val (A,B) = (1,2)
// error: not found: value A
// error: not found: value B

Symbol Literals

Scala comes with a concept of symbols - strings that are interned, that is: two symbols with the same name (the same character sequence), in contrary to strings, will refer to the same object during execution.

Symbols are a feature of many languages: Lisp, Ruby and Erlang and more, however in Scala they are of relatively small use. Good feature to have nevertheless.

Use:

Any literal beginning with a single quote ', followed by one or more digits, letters, or under‐scores _ is a symbol literal. The first character is an exception as it can’t be a digit.

Good definitions:

'ATM
'IPv4
'IPv6
'map_to_operations
'data_format_2006

// Using the `Symbol.apply` method

Symbol("hakuna matata")
Symbol("To be or not to be that is a question")

Bad definitions:

'8'th_division
'94_pattern
'bad-format

Operators in Scala

Packages

User Defined Functions for Hive

Dynamic Invocation

In order to declare subtypes of Dynamic, the language feature dynamics must be enabled, either by importing scala.language.dynamics or by the -language:dynamics compiler option. Users of this Dynamic who do not define their own subtypes do not need to enable this.

Continuations Library

Scala.js

Testing with ScalaCheck

Scope

scalaz