Skip to content

Single line case is multi-line #14721

Closed
@som-snytt

Description

@som-snytt

Compiler version

Scala code runner version 3.1.3-RC1-bin-SNAPSHOT-git-aa59abe -- Copyright 2002-2022, LAMP/EPFL

Minimized code

import scala.util.chaining.given

class C:
  def op: Unit = throw new RuntimeException("a").tap(_ => println("throw a"))
  def handler: Unit = throw new RuntimeException("b").tap(_ => println("throw b"))
  def test: Unit =
    try op
    catch case _: NullPointerException =>
    handler
  end test
  def test1: Unit =
    try op
    //catch case _: NullPointerException =>
    catch
    case _: NullPointerException =>
    end try
    handler
  end test1

@main def test() = println {
  val c = new C()
  c.test
  //c.test1
}
Compilation output
    def test: Unit =
      try op catch {
        {
          case _:NullPointerException => handler
        }
      }
    def test1: Unit =
      {
        try op catch {
          {
            case _:NullPointerException =>
              <empty>
          }
        }
        handler
      }

Expectation

In test, the call to handler is in the case, as opposed to following it as in test1.

This is probably because ExprCaseClause must consume an Expr.

The single line catch case in test1 is not allowed with an empty body before end try.

That may be a feature, but the doc for new control syntax says it must "follow on a single line"; maybe that wording just needs to be tweaked to say "start on the same line".

However, it's also very confusing to read. It doesn't conform to intuitions acquired from optional braces (which doesn't govern this syntax). Normally, the aligned token that is not case incurs the outdent. (And now I see that is not only when the case is aligned with match.)

  def f(x: Int): Int =
    x match
    case 1 =>
    println("no") ; 42
    case _ => 27

I love single line catch case quiet syntax, but would love it more on a single line.

Sample in the wild where it's not obvious what was intended.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions