Scala Cheatsheet
Scalacheat
Language
Thanks to Brendan O’Connor, this cheatsheet aims to be a quick reference of Scala syntactic constructions. Licensed by Brendan O’Connor under a CC-BY-SA 3.0 license.
variables | ||
Good
|
variable | |
Bad
|
constant | |
|
explicit type | |
functions | ||
Good
Bad
|
define function hidden error: without = it’s a Unit-returning procedure; causes havoc |
|
Good
Bad
|
define function syntax error: need types for every arg. |
|
|
type alias | |
vs.
|
call-by-value call-by-name (lazy parameters) |
|
|
anonymous function | |
vs.
|
anonymous function: underscore is positionally matched arg. | |
|
anonymous function: to use an arg twice, have to name it. | |
Good
Bad
|
anonymous function: bound infix method. Use 2 * _ for sanity’s sake instead. |
|
|
anonymous function: block style returns last expression. | |
|
anonymous functions: pipeline style. (or parens too). | |
|
anonymous functions: to pass in multiple blocks, need outer parens. | |
|
currying, obvious syntax. | |
|
currying, obvious syntax | |
|
currying, sugar syntax. but then: | |
|
need trailing underscore to get the partial, only for the sugar version. | |
|
generic type. | |
|
infix sugar. | |
|
varargs. | |
packages | ||
|
wildcard import. | |
|
selective import. | |
|
renaming import. | |
|
import all from java.util except Date. | |
At start of file:
Packaging by scope:
Package singleton:
|
declare a package. | |
data structures | ||
|
tuple literal. (Tuple3 ) |
|
|
destructuring bind: tuple unpacking via pattern matching. | |
Bad
|
hidden error: each assigned to the entire tuple. | |
|
list (immutable). | |
|
paren indexing. (slides) | |
|
cons. | |
same as
|
range sugar. | |
|
Empty parens is singleton value of the Unit type Equivalent to void in C and Java. |
|
control constructs | ||
|
conditional. | |
same as
|
conditional sugar. | |
|
while loop. | |
|
do while loop. | |
|
break. (slides) | |
same as
|
for comprehension: filter/map | |
same as
|
for comprehension: destructuring bind | |
same as
|
for comprehension: cross product | |
|
for comprehension: imperative-ish sprintf-style |
|
|
for comprehension: iterate including the upper bound | |
|
for comprehension: iterate omitting the upper bound | |
pattern matching | ||
Good
Bad
|
use case in function args for pattern matching. | |
Bad
|
“v42” is interpreted as a name matching any Int value, and “42” is printed. | |
Good
|
”`v42`” with backticks is interpreted as the existing val , and “Not 42” is printed. |
|
Good
|
is treated as an existing val, rather than a new pattern variable, because it starts with an uppercase letter. Thus, the value contained within is checked against , and “Not 42” is printed. |
|
object orientation | ||
|
constructor params - is only available in class body |
|
|
constructor params - automatic public member defined | |
|
constructor is class body declare a public member declare a gettable but not settable member declare a private member alternative constructor |
|
|
anonymous class | |
|
define an abstract class. (non-createable) | |
|
define an inherited class. | |
|
inheritance and constructor params. (wishlist: automatically pass-up params by default) | |
|
define a singleton. (module-like) | |
|
traits. interfaces-with-implementation. no constructor params. mixin-able. |
|
|
multiple traits. | |
|
must declare method overrides. | |
|
create object. | |
Bad
Good
|
type error: abstract type instead, convention: callable factory shadowing the type |
|
|
class literal. | |
|
type check (runtime) | |
|
type cast (runtime) | |
|
ascription (compile time) | |
options | ||
|
Construct a non empty optional value | |
|
The singleton empty optional value | |
|
Null-safe optional value factory | |
same as
|
Explicit type for empty optional value. Factory for empty optional value. |
|
|
Pipeline style | |
|
for-comprehension syntax | |
same as
|
Apply a function on the optional value | |
same as
|
Same as map but function must return an optional value | |
same as
|
Extract nested option | |
same as
|
Apply a procedure on optional value | |
same as
|
Apply function on optional value, return default if empty | |
same as
|
Apply partial pattern match on optional value | |
same as
|
True if not empty | |
same as
|
True if empty | |
same as
|
True if not empty | |
same as
|
Zero if empty, otherwise one | |
same as
|
Evaluate and return alternate optional value if empty | |
same as
|
Evaluate and return default value if empty | |
same as
|
Return value, throw exception if empty | |
same as
|
Return value, null if empty | |
same as
|
Optional value satisfies predicate | |
same as
|
Optional value doesn't satisfy predicate | |
same as
|
Apply predicate on optional value or false if empty | |
same as
|
Apply predicate on optional value or true if empty | |
same as
|
Checks if value equals optional value or false if empty |