简体   繁体   English

Scala调用链接

[英]Scala invocation chaining

Would you write: 你会写:

(ontDrugFormXml \ "VPID").headOption map
    (id => graph.addE(vertex, searchDmdVertex(graph, "VMP", id.text), "has")) orElse
    (throw new IllegalStateException("VPID required"))

or: 要么:

  (ontDrugFormXml \ "VPID").headOption
    .map(id => graph.addE(vertex, searchDmdVertex(graph, "VMP", id.text), "has"))
    .orElse(throw new IllegalStateException("VPID required"))

Which one is proper to you? 哪一个适合你?
Can't have the answer on http://docs.scala-lang.org/style/method-invocation.html 无法在http://docs.scala-lang.org/style/method-invocation.html上找到答案

Like this: 像这样:

(ontDrugFormXml \ "VPID").headOption map { id =>
    graph.addE(vertex, searchDmdVertex(graph, "VMP", id.text), "has")
} orElse (throw new IllegalStateException("VPID required"))

The scala style guide requires infix notation for methods like map that take a function argument. scala样式指南需要使用带有函数参数的map方法的中缀表示法。 Parameters should be on the same line as the opening brace, eg next to map . 参数应与开括号位于同一行,例如在map旁边。 orElse should be one the same line as the value it takes. orElse应该与它所需的值相同。

Infix, the first option. Infix,第一选择。 I always use infix notation for orElse. 我总是使用中缀符号表示orElse。

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

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