简体   繁体   English

scala:以下两个是否相同

[英]scala: whether the following two are the same

Code piece 1 代码段1

maps foreach { case (k, v) =>
  // do something
}

code piece 2: 代码段2:

maps foreach { 
  case (k, v) => {
    // do something
  }
}

I am new to scala. 我是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 . 但是不幸的是,它们都没有考虑到Scala风格指南的建议。

  1. Omitting dots and using spaces is not recommended . 不建议省略点并使用空格。

  2. Always omit braces in case clauses . case子句中总是省略大括号

  3. case may be present on same line or on the next line: it depends on the contents of // do something . case可能出现在同一行或下一行:它取决于// do something的内容。

So the original code should be formatted as 因此,原始代码的格式应为

maps.foreach {
  case (k, v) => // do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM