which both seem to do the same thing, although the REPL gives slightly diffrent messages after you type each in.
I don't think it's just syntactic sugar because if you just type in the name of the function in each case, it comes back with:
scala> greater
<console>:8: error: missing arguments for method greater in object $iw;
follow this method with `_' if you want to treat it as a partially applied function
greater
^
With def, you declare a method. The expression won't be evaluated until you call the method.
With val, you create a value (a constant). The expression is evaluated at the point where you declare it.
But you've written some code that makes it a bit confusing.
Note that the val fun in your method greater2 is a function: it has the type Int => Boolean. The syntax:
is syntactic sugar for this:
So, in that case, fun is really a Function1 object with an apply method that gets called when you make a "call" on fun.
Note by the way that an expression like if(x > n) true else false is overly verbose. You can just write x > n, which is by itself an expression that evaluates to a Boolean:
To add, With 'def' you are creating a method which means you cannot pass it around as first class value whereas 'val's containing functions are first class values so you can pass it around.
Thats why you are getting the following error since the method is not passed enough arguments hence it is not a method call and also it cannot be assigned to the REPL generated variable since it is not a first class value:
If we follow this method with '_', a function is created which can be passed around/assigned to a variable.