Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are parenthesis required for invoking methods with default arguments?

Tags:

scala

I have a side-effect free method with default arguments that I'd like to invoke without parenthesis, e.g.:

scala> def foo(x: Int = 1) = 42
foo: (x: Int)Int

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

scala> foo()
res3: Int = 42

Is this intentional, or just a temporary limitation?

like image 337
Stephen Haberman Avatar asked Jan 03 '13 18:01

Stephen Haberman


People also ask

Why do functions use parentheses?

() (parentheses) They are used to contain a list of parameters passed to functions and control structures and they are used to group expressions to control the order of execution. Some functions have no parameters and in this case, the space between parentheses is blank.

Why do you need parentheses in Python?

Broadly speaking, the primary use of parentheses in Python is to call an object. That is the reason why standard parentheses are sometimes called the "call operator." Aside from their main use, parentheses are also used to define generator expressions.

Why do we use parentheses in JavaScript?

Description. The grouping operator consists of a pair of parentheses around an expression or sub-expression to override the normal operator precedence so that operators with lower precedence can be evaluated before an operator with higher precedence.

What happens when a method is accessed without the () parentheses?

When we call a function with parentheses, the function gets execute and returns the result to the callable. In another case, when we call a function without parentheses, a function reference is sent to the callable rather than executing the function itself.


1 Answers

This is probably intentional so you don't get parameter blocks disappearing on you:

def foo(x: Int = 2)(y: Int = 4) = x*y

foo(3)    // What does this mean???
like image 84
Rex Kerr Avatar answered Sep 19 '22 00:09

Rex Kerr