3

By default scalafmt will reformat code like this

myList(elem => 
  elem
    .doThing
    .doOtherThing
)

to look like this

myList(
  elem => 
    elem
      .doThing
      .doOtherThing
)

How can it be configured to leave the anonymous function parameter on the same line? Note that it already does this if you use curly brackets instead of parens.

kag0
  • 5,624
  • 7
  • 34
  • 67
  • 2
    If a multi-line lambda expression is desired, use curly braces, not parens. – Yawar Sep 27 '19 at 03:30
  • I understand that is a common style convention, but sometimes using parens over curly brackets can catch typos at compile time. For example https://pastebin.com/RaM7zeD5. If you're interested in a more practical case, consider akka http inner route concatenation where you might miss a `~`. – kag0 Sep 27 '19 at 04:26
  • Your example produces a warning: `warning: a pure expression does nothing in statement position; multiline expressions might require enclosing parentheses`. It's a really good idea to make warnings fatal, and in fact turn on as many warnings as possible :-) – Yawar Sep 27 '19 at 18:42
  • I would still like to know the answer to the original question. But it's good to know this case can produce a warning. I do have fatal warnings on pretty much all the time, but don't see an error, what's the flag to enable the warning you got? – kag0 Sep 27 '19 at 21:58
  • See https://tpolecat.github.io/2017/04/25/scalac-flags.html , in my opinion almost all of these warnings should be turned on to keep a project under control. Specifically the one in question here is: `-Ywarn-value-discard` – Yawar Sep 28 '19 at 00:29

0 Answers0