Basic types

Floating-point types are the same two, Float and Double; no surprise there.

Character types, Char and String, are spelled the same, but Scala’s Char is actually a 16-bit unsigned Unicode character, and thus String is in UTF-16.

Boolean type is spelled a bit differently and, most importantly, its values are all lowercase:

Haskell: Bool -- True and False
Scala: Boolean // true and false

() (the type) is spelled Unit (but the value is still ()).

Save for String, all of the above are value types. They have a nice property: they can’t be null. Reference types, on the other hand, can. This distinction is mirrored in the class hierarchy of Scala: value types are subtypes of AnyVal, while reference types are subtypes of AnyRef. Finally, all the types are subtypes of Any.

Polymorphism and type constraints

Scala doesn’t rely on hierarchy of typeclasses to overload common functions and operators — here, you can just implement method + and be done with it. Thus, type constraints are used only to limit subtyping.

For example, if we have class C2 that extends class C1 which, in turn, extends AnyVal, the following code will work on both C1 and C2, but not on AnyVal:

Variance annotations

Another thing that you never had to deal with in Haskell is co- and contravariance (because Haskell doesn’t have subtyping). If you want your trees to be covariant, just add plus before the type variable:

foldLeft in Scala (also spelled /:) is the same as flip $ foldl' in Haskell, i.e. it takes the base value as the first argument and the function as the second. There’s also reduceLeft which is like foldl1, only strict; foldRight a.k.a. :\ (arguments are flipped, too); and reduceRight.

Streams

Iterators

…are imperative counterparts of streams. Iterator[+A] is a trait specifying two methods, hasNext: Boolean and next: A. There’s also BufferedIterator[+A] specifying method head: A, which is just like next, but doesn’t advance the iterator.

Iterators implement all of the usual HOFs.

You can turn your own class into iterator by extending the trait, or you can create it ad-hoc:

Imperative programming

Scala has all the traditional imperative control structures: while and do-while loops (for loops are present as for-comprehensions), ifs with optional else branches, return and so on. Note, though, that there’s no break and continue for loops.

Minimal working program

You’re done with theory, now on to writing some real code! Oh wait, you don’t know how to compile stuff yet. Well, just head over to tpolecat’s blog and read “Setting up to write some Scala” — it’ll get you all set up in no time. He provides example program, too, so my job here is done.

Note of thanks

I’d like to thank kind people on scala@conference.jabber.ru for answering my confused questions and explaining how things should be done. The same goes for attendees of #scala and #scalaz on Freenode — you people rock!

Update 08.08.2015:

clarify that () (the type) is spelled Unit, but the value is still () (thanks tpolecat)