Download Getting Started with Scala 26-Jul-16

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Transcript
Simple method definitions

def isEven(n: Int) = {
val m = n % 2
m == 0
}



def isEven(n: Int) = n % 2 == 0



The result is the last value (in this case, a Boolean)
This is really kind of poor style; the extra variable isn’t needed
This is much better style
The result is just a single expression, so no braces are needed
def countTo(n: Int) {
for (i <- 1 to 10) { println(i) }
}



It’s good style to omit the = when the result is ()
If you omit the =, the result will be ()
You need either braces or an =; you can’t leave out both