-2

In the Coursera course "Functional Programming Principles in Scala" Week 1 sample assignment (https://www.coursera.org/learn/progfun1/programming/xIz9O/example-assignment) Martin Ordersky mentions in one of his comments (on line 44 of the ListsSuites.scala test) that

   * In Scala, it is allowed to pass an argument to a method using the block
   * syntax, i.e. `{ argument }` instead of parentheses `(argument)`.

So when assigning a function definition in Scala would it be equivalent to say:

def foo = ()

and

def foo = {}
nobody
  • 7,803
  • 11
  • 56
  • 91

4 Answers4

3

That's defining a method/def - the comment is referring to when calling the def

scala> def foo(arg1: Int) = arg1 + 1
foo: foo[](val arg1: Int) => Int

scala> foo(1) // round parenthesis
res0: Int = 2

scala> foo{1} // bracket parenthesis
res1: Int = 2

Also note the body of the def does not need to be enclosed in parenthesis. So using your example, all of the following are equivalent:

def foo = (???) // ??? means implementation not yet defined
def foo = {???}
def foo = ???
Tanjin
  • 2,442
  • 1
  • 13
  • 20
2

{} or ({}) refers to block of code.

{
  statement1
  statement2
  statement3
}

and the last statement would be returned.

scala> val x = { 
     |  val x = 4 
     |  x + 1
     | }
x: Int = 5

While () is for one line expression.

When are {} and () interchangeable

1.invoking a method with one param

scala> def printlnme(x: String) = { println(x)}
printlnme: (x: String)Unit

scala> printlnme("using parenthesis")
using parenthesis

scala> printlnme{"using braces"}
using braces

Not with a method with multiple params,

scala> def add(x: Int, y: Int) = { x + y }
add: (x: Int, y: Int)Int

//can also be (x + y) because one line expression
//scala> def add(x: Int, y: Int) = ( x + y )
//add: (x: Int, y: Int)Int

scala> add(1, 2)
res3: Int = 3

scala> add{1, 2}
<console>:1: error: ';' expected but ',' found.
foo{1, 2}
     ^

2.one line expression

In this example I'm only printing the input.

scala> List(1, 2, 3).foreach { x => println(x) }
1
2
3

scala> List(1, 2, 3).foreach ( x => println(x) )
1
2
3

Or, say one line map function.

scala> List(1, 2, 3) map { _ * 3}
res11: List[Int] = List(3, 6, 9)

scala> List(1, 2, 3) map ( _ * 3 )
res12: List[Int] = List(3, 6, 9)

But () alone can not be used with multiline statements

scala> :paste

List(1, 2, 3) foreach ( x =>
     val y = x * 28
     println(y)
)

<console>:2: error: illegal start of simple expression
     val y = x * 28
     ^

you still need ({}) to be able to use parenthesis for multiline.

scala> :paste

List(1, 2, 3) foreach ( x => {
     val y = x * 28
     println(y)
})

28
56
84
prayagupa
  • 30,204
  • 14
  • 155
  • 192
1

Specifically in your case: yes, they will both result in foo: Unit

In many cases they will effect the same due to both evaluating to expressions, but strictly speaking they are different. {} is an empty block which evaluates to Unit because it's (absent) last statement evaluates to Unit, whereas () is the default value of the Unit type.

This question has come up long ago, and imo the key point regarding your particular question is that you cannot use the {} syntax with functions that take more than 1 argument (non-curried). Extensive coverage of other cases can be found here. Generally those two are different; they just frequently happen to do the same thing.

Community
  • 1
  • 1
midor
  • 5,487
  • 2
  • 23
  • 52
1

To give you brief answer, both are representation of memory blocks but it differs in terms of how much the blocks can accommodate.

{} is for multiple line definitions

 Seq(1, 2, 3, 4, 5).map { x =>
      x + 1
      x - 1
    }

() is for single line definition

   Seq(1, 2, 3, 4, 5).map(x =>
      x + 1
      x -1)

The above code will give you compiler error as it can accommodate only single line .But you can also use {} inside () to accommodate more than one lines as follows

Seq(1, 2, 3, 4, 5).map(x => {
  x + 1
  x - 1
})
Balaji Reddy
  • 5,576
  • 3
  • 36
  • 47