Scala 中的换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12335388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:30:32  来源:igfitidea点击:

Line continuation character in Scala

scalanewline

提问by opensas

I want to split the following Scala code line like this:

我想像这样拆分以下 Scala 代码行:

ConditionParser.parseSingleCondition("field=*value1*").description 
  must equalTo("field should contain value1")

But which is the line continuation character?

但哪个是行继续符?

回答by kiritsuku

Wrap it in parentheses:

用括号括起来:

(ConditionParser.parseSingleCondition("field=*value1*").description 
  must equalTo("field should contain value1"))

Scala does not have a "line continuation character" - it infers a semicolon always when:

Scala 没有“行继续符”——它总是在以下情况下推断分号:

  • An expression can end
  • The following (not whitespace) line begins not with a token that can start a statement
  • There are no unclosed (or [found before
  • 一个表达式可以结束
  • 以下(非空白)行不以可以开始语句的标记开头
  • 之前没有未关闭([发现的

Thus, to "delay" semicolon inference one can place a method call or the dot at the end of the line or place the dot at the beginning of the following line:

因此,要“延迟”分号推断,可以将方法调用或点放在行尾或将点放在下一行的开头:

ConditionParser.
parseSingleCondition("field=*value1*").
description must equalTo("field should contain value1")

a +
b +
c

List(1,2,3)
  .map(_+1)