Use {} or () when calling a function?

Question: which one is right below?

case 1

list.map(item => {
  val newItem = process(item)
  newItem
})

case 2

list.map{item => {
  val newItem = process(item)
  newItem
}}

Answer: both are ok

In scala. you could use () or {} to wrap your parameter listing. When the function body is a case statement, using {} becomes much more understanding.


such as the following:


using {} with case statement

.flatMap {
  case 1 => Sync[M].delay {
    println(1);
  }
  case 2 => Sync[M].pure(2)
}

using () with case statement

.flatMap (
  {
  case 1 => Sync[M].delay {
    println(1);
  }
  case 2 => Sync[M].pure(2)
  }
)
Last updated: 2024-04-14 04:46:25scala
Author:Chaolocation:https://www.baidu.com/article/27
Comments
Submit
Be the first one to write a comment ~