Code piece 1
maps foreach { case (k, v) =>
// do something
}
code piece 2:
maps foreach {
case (k, v) => {
// do something
}
}
I am new to scala. Just wonder whether the above two pieces of codes are the same or not? which one is better?
Thanks
Yes, those two pieces of code are same.
But unfortunately none of them takes into account the recommendations of the Scala style guide.
Omitting dots and using spaces is not recommended.
case
may be present on same line or on the next line: it depends on the contents of // do something
.
So the original code should be formatted as
maps.foreach {
case (k, v) => // do something
}