scalacheat variables | |
| variable |
GOOD 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 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 | 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. |
same as | range sugar. |
(empty parens) | sole member of the Unit type (like C/Java void). |
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 | |
same as | 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 GOOD | type error: abstract type instead, convention: callable factory shadowing the type |
| class literal. |
| type check (runtime) |
| type cast (runtime) |
| ascription (compile time) |