A few years back I dipped into some Scala as a hobby language. Recently, in order to get a quick overview of Spark I did the 'Big Data Analysis with Scala and Spark' from Coursera. It's a great course. But, one aspect I found challenging was just getting my head around Scala syntax again. Some of it, yeah the basic stuff can be counter-intuitive depending on your perspective.
But here is what I find is. If a function has no return type in the function definition and no equals it means Unit is implicitly the return type. Example:
:12: warning: enclosing method addNumbers has result type Unit: return value discarded return a + b It will compile but nothing will be returned:
1. Method / Function Definition
Typing on the right rather than the left. Consider this simple function definition:def sayHello(param: String): String = { "Hello" + param }Javaholics will note:
- The return is specified at the end of the method definition, rather than the beginning.
- The type of the parameter is specified after the parameter name rather than before.
- Before the function body there is a =
- There are two colons (:), one between the parameter and the type and one before the return type.
2. Unit
Google "Unit" and you be quickly told you that Unit is the Scala's version of Java void. But, Java’s void is a keyword. Scala’s Unit is a final class which only has one value: () - which is like an alias for no information. Unit indicates a method returns nothing and therefore has side effects, something we don't want to do much of in Scala. So is that counter intuitive? No.But here is what I find is. If a function has no return type in the function definition and no equals it means Unit is implicitly the return type. Example:
def procedure { println "This String is not returned" } procedure: ()UnitBig deal? Of course not. But what about:
def procedure { "This String is not returned" }Expect the String to be returned, it wont be. How about this?
def addNumbers(a: Integer, b: Integer) { return a + b }This will give a compile warning:
def addNumbers(a: Integer, b: Integer) { a + b }will give no compile warning and will also return nothing.
3. Underscore
In anonymous Scala functions, _ is like Groovy's it. In Groovy we can to multiple all numbers between 1 and 5 we can do:(1..5).collect {it * 2}In Scala we can do:
(1 to 5).map{_*2}However, in Scala, the second time _ is referenced, it refers to the second parameter
val ns = List(1, 2, 3, 4) val s0 = ns.foldLeft (0) (_+_) //10
4. Passing anonymous functions.
Pass one anonymous function and you don't need any curly parenthesis. Pass two and you do.def compose(g:R=>R, h:R=>R) = (x:R) => g(h(x)) val f = compose({_*2}, {_-1})
5. Arity-0
When a method has no arguments, (arity-0), the parentheses can be omitted in invocationsize() ... size // do it like thisBut this technique should never be used when method has side effects. So,
queue.size // ok println // not ok do println()
6. Declare parameter types
Function defiinitions / Method definition have to declare parameter types but function literals don’t.def addNumbers(a, b): Number {:1: error: ':' expected but ',' found.
7. Ternary Operator
There is no ternary operator in Scala. There is one in Java, Groovy, JavaScript. Python 2.5 added support for it. Instead you can do if else on one line and since if / else is an expression you can return a value. For example: In Java we would do:(eurovision.winner == "Ireland") ? "Yippee" : "It's a fix"Scala, it's:
if (eurovision.winner == "Ireland") "Yippee" else "It's a fix"