Skip to content

Clarification of intended ’if … then’ new control syntax interaction with newlines #12757

Closed
@djneades

Description

@djneades

Compiler version

3.0.0

Minimized code

This compiles:

val x = Some(10)

def f =
   if x.exists(x => x == 10) then
      println("Yes")
   else
      println("No")

But this does not:

val x = Some(10)

def f =
   if x.exists
        (x => x == 10) then
      println("Yes")
   else
      println("No")

Expectation

Given that Scala 3 allows method calls like this in other contexts:

foo
  (arg)

the if x.exists … in the second example above seems at least intuitively plausible, even though it doesn’t compile. Clarification as to whether or not that latter example should be considered valid would be appreciated. (The New Control Syntax page doesn’t obviously settle the matter.)

My doubt here arises from:

  1. whether the newline after the if x.exists should terminate the if condition, as it currently seems to do in Scala 3.0.0;

  2. or whether the if condition should instead be considered to continue up until the following then, the newline before the indented argument to x.exists being treated as it would be in other contexts.

Interestingly, this does compile:

val x = Some(10)

def f =
   if
      x.exists
         (x => x == 10) then
      println("Yes")
   else
      println("No")

As does:

val x = Some(10)

def f =
   if
      x.exists
         (x => x == 10)
   then
      println("Yes")
   else
      println("No")

Note that this question arose in this Scalafmt-related comment.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions