2

Just picking up Scala. Found it surprisingly tricky to pick up the fundamentals of the language despite many tutorials online. Can someone explain the the nuances of the following code? (using scala 2.10.2)

Case 1

scala> def greet = println("helloo")
greet: Unit
scala> greet
helloo
scala> greet()
<console>:9: error: Unit does not take parameters
          greet()
               ^

Case 2

scala> def greet = ()=>println("helloo")
greet: () => Unit
scala> greet
res31: () => Unit = <function0>
scala> greet()
helloo

Case 3

scala> def greet() = println("helloo")
greet: ()Unit
scala> greet
helloo
scala> greet()
helloo

However it is surprising that there are no clear conceptual description of the Scala language Sorry if this is an utter newbie question!

vecktorking
  • 109
  • 1
  • 6
  • Almost -1 for "there are no clear conceptual description of the Scala language". The rollover definition of down vote is "does not show any research effort." But clearly you tried something, you just didn't try, um, google. – som-snytt Aug 02 '13 at 06:24
  • @som-snytt I dont mean to be a wise ass here but you should have noticed that I did state the following in my question: **despite many tutorials online**. It is more constructive to post a link here where this question has been tackled before. – vecktorking Aug 02 '13 at 14:37
  • I was exaggerating for rhetorical effect, sorry to get your goat. My point is that, while Scala has nuances, a lack of docs and blogs is not one of its shortcomings. http://www.artima.com/pins1ed/composition-and-inheritance.html#10.3 and http://stackoverflow.com/q/12334936/1296806 on paramless def. HTH. FWIW, it remains tricky. – som-snytt Aug 02 '13 at 22:54

1 Answers1

8

Scala can have an arbitrary number of parameter blocks, not just one. So:

def f(i: Int) = i + 2
def g(i: Int)(j: Int) = i + j
def h(i: Int)(j: Int)(b: Boolean) = if (b) i + j else i * j

all works. Parameter blocks are a way to organize parameters and signal to the compiler how to handle the translation from a method (which defs are) into a function (which is a method that you can pass as an argument).

Now, if you have empty parameter blocks you can just omit them for brevity:

def a()()() = println("Hi") // Do nothing!
a()()()                     // Full call
a()                         // Drop some
a                           // Drop all of them

All of the calls to a do the same thing. But you can't add more empty parameter blocks than belong--those are interpreted as calls to whatever is returned by the method.

a()()()()                   // No go, calls `()` on `Unit` which doesn't exist

So this covers cases 1 and 3 of yours.

Now, you can also have a method return a function. A function maps arguments (just one block!) to an output, but unlike methods, you can pass it around. (Scala is good at automatically wrapping methods into functions when such is needed.)

So

def b = () => println("Hi")

says that each time you call b, you should create a function that takes no parameters and returns whatever println("Hi") returns. In fact, it returns Unit, i.e. no return value--it actually is a value, but it's always the same entity: (). This avoids having to have special cases for procedures/methods returning void and methods with a return value; everything has a return value (at least conceptually). Anyway, if you have one:

val c = b

then you can call it:

c()    // Will print "Hi"

or you can just create and call all in one go

b()    // The `b` part creates the function, `()` calls it.

A tour through e.g. Programming in Scala will cover all the basics you'll need in this regard. (I liked having a paper copy, even if one does have to pay for it.)

Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
  • Thanks you for the clear succinct response. This does clear up my concepts quite a bit. Coming from a lengthy experience in Java , Scala's depth and breadth of features in breathtaking! – vecktorking Aug 01 '13 at 23:31
  • It wouldn't push your rep into six digits, but have you considered just posting some live action tutorials on youtube? Then you could say, "I discuss this at length in my tutorial...", etc. – som-snytt Aug 02 '13 at 06:30
  • @som-snytt - I'm more of a writer than an actor. Someone else can do the adaptation for YouTube. – Rex Kerr Aug 02 '13 at 11:36
  • @vecktorking: Actually, compared to Java, Scala is *much* simpler. It has less corner cases, less exceptions, less arbitrary decisions, and, yes, actually not even that much more features. Maybe even less, e.g. it doesn't have primitives, it doesn't have statics, it (almost) doesn't have operators. For example, Java has nested classes but not nested methods or nested packages. Scala just has nested everything. In Java, some things can be abstract, some things can be parametric, some things can be both, some things can be neither. In Scala, everything can be abstract and parametric. – Jörg W Mittag Aug 03 '13 at 00:22
  • The Scala Language Specification is much smaller than the Java Language Specification. (Admittedly, they are written in a somewhat different style.) – Jörg W Mittag Aug 03 '13 at 00:23