1

I am not sure that this might is a Scala pattern matching question. I saw the following examples and it uses Scala pattern matching.

def sum(ints: List[int]): Int = ints match {
  case Nil => 0
}

def sum(ints: List[int]): Int {
  ints match {
    case Nil => 0
  }
}

what's the difference between these two examples?

Thank you :)

flavian
  • 28,161
  • 11
  • 65
  • 105
cosmir17
  • 57
  • 7
  • They're the same. Although in the former example, you won't be able to add another line of code without adding the curly brackets. – Yuval Itzchakov Aug 27 '16 at 12:00
  • @cosmir17 Off topic advice, reading poorly indented code is like reading a letter written with the left foot. http://mrbool.com/importance-of-code-indentation/29079 – flavian Aug 27 '16 at 12:23
  • 1
    The second form will not compile – Pablo Francisco Pérez Hidalgo Aug 27 '16 at 12:48
  • 1
    Possible duplicate of [What is the formal difference in Scala between braces and parentheses, and when should they be used?](http://stackoverflow.com/questions/4386127/what-is-the-formal-difference-in-scala-between-braces-and-parentheses-and-when) –  Aug 27 '16 at 12:52
  • Note that the second version is _very_ strange, I think it may make sense to include `=` before the first curly brace `{` and only then consider the difference. Because right now, it's an `Int` type with some overrides if I'm reading the code correctly. Or maybe something else, but definitely not function definition. – VasiliNovikov Aug 28 '16 at 09:31

2 Answers2

1

In Scala if you are doing pattern matching as the first expression inside a function, you can write the syntax in two ways, but they are equivalent.

Note: here we are starting directly with pattern matching:

def toInt(optStr: Option[String]): Option[Int] = optStr match {
 case Some(str) = Some(str.toInt)
 case None =>  None
}

Another way to put it is that you are doing same thing inside multiline brackets.

 def toInt(optStr: Option[String]): Option[Int] = {
  //same pattern matching inside multiline brackets
  optStr match {
    case Some(str) = Some(str.toInt)
    case None => None
  }

}
jub0bs
  • 60,866
  • 25
  • 183
  • 186
Nagarjuna Pamu
  • 14,737
  • 3
  • 22
  • 40
  • Just to be clear, "if you are doing pattern matching as the ***only*** expression inside a function, you can write..." and of course this applies to any other expression. – grzesiekw Aug 27 '16 at 12:09
  • @grzesiekw yes !!! this applies to any single expression. if its multiple expressions you need to use {}. – Nagarjuna Pamu Aug 27 '16 at 12:12
1

Curly braces are the way Scala uses to glue together a group of expressions into one whose result is the result of the last expression in the block.

e.g:

{
    val x = 3
    x*2
}

Is an expression whose result is 6.

Writing a single expression within curly braces is redundant and would only make sense for aesthetic purposes.

Going back to your pattern matching:

ints match {
  case Nil => 0
}

Is an expression as any other so you can write it within curly braces or not.

{
    ints match {
      case Nil => 0
    }
}

Scala lets you define your methods body as a group of expressions:

def methodName(params...) { exp0; exp1; .., resExp } // Return type will be Unit.

NOTE That Scala's type inference will determine methodName return type as Unit.

Or as = with an expression at its right side, which could also be a group of expressions this way glued by curly braces.

def methodName(params...): Type = expression
def methodName(params...) = expression //Return type will be the expression's type.

So you can write it in these 3 forms:

// `=` with a single expression at is right side.
def sum(ints: List[Int]): Int = ints match {
  case Nil => 0
}

// `=` with a glued sequence of expressions at is right side.
def sum(ints: List[Int]): Int = {
    ints match {
      case Nil => 0
    }
}

// A glued sequence of expressions at is right side, but its return type will be Unit!
def sum(ints: List[Int]) {
    ints match {
      case Nil => 0
    }
}