#Getting started with Scala - Guide
- Standalone distribution
- using SBT/maven - no need to install, the compiler and standard library is just another jar dependency of your build
scalac
- compiler, can be used manually, outputs .class-files that can be run on the JVMscala
- interactive scala shell (REPL - Read Eval Print Loop)- SBT - the Scala build tool (what we will be using)
- Possible alternatives
- Maven - scala-maven-plugin
- Gradle - Scala plugin
- A bit of a threshold - can seem arcane on first glance, don't worry
build.sbt
+project/*stuff*
- Interactive shell
- Dependency management -
libraryDependencies += "com.package" %% "artifact" % "1.2.0"
- Easily extendable - everything in the build description is Scala code
- Install - binary from homepage, Linux/OSX - package managers?
- essential tasks: compile, test, clean, run
- Scala IDE - eclipse based
- IntelliJ IDEA
- Vim, Emacs, Atom, Sublime - ensime for highlighting etc.
val
andvar
- immutable (likefinal
) and mutable- Type inference -
val a: String = "Hello"
==val a = "Hello"
- Classes
class A { }
- constructor param -
class A(param: String)
- Fields
- private
class A(field: Int) { ...uses field... }
,class A { private val field = 5 }
- public -
class A(val field: Int)
,class A { val field = 5 }
- private
- Methods
class A() { def method(param: Int): String = { "hello" } }
- Case classes - like immutable java Beans -
case class A(a: String, b: Int)
- Singletons - allow for static methods
object A { }
- Runnable main -
def main(args: Array[String]): Unit = { ... }
- Trait ≈ interface -
trait MyTrait { def method: Int }
- Everything is an expression, multi-line blocks "return" the last statement
- Everything can be nested - method in method, class in method, etc.
- Minimal collections
Vector(1,2,3)
,List(1,2,3)
,Map("a" -> 1, "b" -> 2)
list.foreach(item => println(item))
List("one", "two", "three").map(item => item.length) == List(2, 2, 4)
- for-comprehensions
for { i <- 0 to 6 } println(i)
val lengths = for { word <- List("one", "two", "three") } yield word.length
- Prefer immutability unless there is a reason not to
- Always hide immutability if possible
Scala is very un-opinionated - let the developers choose the tools that fits best - this however makes getting started a bit harder than a language that has exactly one way to do one thing.
Be careful about trusting help mentioning "idiomatic" which might mean very different things to different people - is not always the same as important/useful/practical/performant/easy to understand.
The community - essentially two camps, one more focused on FP and advanced concepts around that, the other one is less homogenic - the camps do however overlap.
Unless really interested in FP don't get stuck on struggling with monads, applicatives, category theory (ScalaZ, cats) - you can be productive and have lots of fun with Scala without it. Of course if you like that stuff - go nuts!
The same goes for any of the advanced stuff - macros, higher kinded types etc. but make sure you really know the swimming basics before going to the deep end of the pool!
- Books
- Atomic Scala (really good except for the setup part, which is a bit messy, can be skipped after this workshop!)
- Scala for the impatient - from the eyes of a Java dev (dated and focuses on a bit weird things)
- Programming in Scala - Martins Scala book - (dated but very thorough)
- List of more books
- Online
- Scaladocs
- Scala for Java Programmers
- scala-lang.org tutorials
- Twitter Scala School
- Big Data University - Scala introduction in the form of short videos - requires signup but is free
- Coursera - Functional Programming Principles in Scala - bi-annual, next time: September
- Style guides
- Getting help
- Stack overflow
- Stack overflow FAQ
- To search for Scala symbols such as "=>" in Stack Overflow, you can use symbolhound search
- Scala user mailing list
- Scala user gitter channel - chat
List of Awesome Scala Libraries
- Web: Play Framework, Scalatra, Akka-HTTP
- Server stuffs: Finagle, Akka
- Big/Fast data: Apache Spark