1

In Javascript I can describe a function like so

function showString(){ console.log("this is a string") }; 

Such that in the console there is a strict difference between the function and the executed function

> function showString(){ console.log("this is a string") }; 
> showString
function showString(){ console.log("this is a string") }
> showString()
this is a string 

In Scala I am now doing the same thing;

def showname() = println("this is a string")

However, when I run this is in the console, it always seems to execute the function instead of also being able to just pass the function around:

scala> def showname() = println("this is a string")
showname: ()Unit
scala> showname // I am expecting a function, not an executed function
this is a string
scala> showname()
this is a string // I am expecting an executed function

Is Scala handling functions differently? Is my expectation wrong?

cantdutchthis
  • 31,949
  • 17
  • 74
  • 114

2 Answers2

4

showname is actually a method, not a function, if you want to get a function you can use the underscore syntax:

scala> def showname() = println("this is a string")
showname: ()Unit

scala> showname
this is a string

scala> showname _
res1: () => Unit = <function0> 

Which returns a <function0> from Unit to String:

scala> res1
res2: () => Unit = <function0>

scala> res1()
this is a string

You can also check that showname is a method if you modify its signature and try to invoke it without parameter:

scala> def showname(s: String) = println("this is a string")
showname: (s: String)Unit

scala> showname
<console>:9: error: missing arguments for method showname;
follow this method with `_' if you want to treat it as a partially applied function
              showname

For differences between functions and methods there's this great SO post.

Community
  • 1
  • 1
Ende Neu
  • 15,581
  • 5
  • 57
  • 68
  • I think *function object* is a better name for the thing you get after _ application (though, not ideal, since object in scala carries yet another meaning -- singleton). – om-nom-nom Aug 29 '14 at 13:51
  • @om-nom-nom I see your point but I find it very confusing to be honest, also I never read this kind of naming (or maybe I didn't notice), I always sticked to function. – Ende Neu Aug 29 '14 at 14:17
  • Okay :-) I only proposed alternative. *I never read this kind of naming* well, [it's relatively popular](https://www.google.com/search?q="function+object"+scala&oq="function+object"+scala). – om-nom-nom Aug 29 '14 at 14:29
  • And to think that I also read [this](http://stackoverflow.com/questions/9737352/what-is-the-apply-function-in-scala) question many times, I probably never actually put attention to the naming, thanks for noticing. – Ende Neu Aug 29 '14 at 14:35
1

That's not a function, that's a method. This is a function:

val showname = () => println("this is a string")

showname
// => res0: () => Unit = <function0>

showname()
// this is a string

And as you can see, the function behaves just like you would expect it from a function.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653