scalacheat variables |
|
|
variable |
GOOD |
constant |
|
explicit type |
functions |
|
GOOD |
define function hidden error: without = it’s a Unit-returning procedure; causes havoc |
GOOD |
define function syntax error: need types for every arg. |
|
type alias |
|
call-by-value call-by-name (lazy parameters) |
|
anonymous function |
|
anonymous function: underscore is positionally matched arg. |
|
anonymous function: to use an arg twice, have to name it. |
GOOD |
anonymous function: bound infix method. Use |
|
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. |
|
declare a package. |
data structures |
|
|
tuple literal. ( |
|
destructuring bind: tuple unpacking via pattern matching. |
BAD |
hidden error: each assigned to the entire tuple. |
|
list (immutable). |
|
paren indexing. (slides) |
|
cons. |
|
range sugar. |
|
sole member of the Unit type (like C/Java void). |
control constructs |
|
|
conditional. |
|
conditional sugar. |
|
while loop. |
|
do while loop. |
|
break. (slides) |
|
for comprehension: filter/map |
|
for comprehension: destructuring bind |
|
for comprehension: cross product |
|
for comprehension: imperative-ish |
|
for comprehension: iterate including the upper bound |
|
for comprehension: iterate omitting the upper bound |
pattern matching |
|
GOOD |
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 |
GOOD |
|
object orientation |
|
|
constructor params - private |
|
constructor params - public |
|
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 |
type error: abstract type instead, convention: callable factory shadowing the type |
|
class literal. |
|
type check (runtime) |
|
type cast (runtime) |
|
ascription (compile time) |