Skip to content

Terminal colon for templates with empty bodies should be permitted #12185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
propensive opened this issue Apr 22, 2021 · 4 comments
Closed

Terminal colon for templates with empty bodies should be permitted #12185

propensive opened this issue Apr 22, 2021 · 4 comments

Comments

@propensive
Copy link
Contributor

Compiler version

Scala 3.0.0-RC2

Minimized example

trait Foo:

Output

Error: indented definitions expected, eof found

Expectation

Template definitions which end in a : but have empty bodies turn out to be quite a common occurrence during development, usually because the non-compiling body gets temporarily commented out. But the presence of the : combined with the empty body is considered erroneous by the parser.

This creates a small but disproportionate amount of friction. Seeing as commenting-out a template's body is as simple as pressing Ctrl+/, it's very easy to do, but I find that I make the same mistake of forgetting to also delete the : over and over again. And then I also have to add the : back when I reinstate the template body.

I would prefer the : to be optional for templates with empty bodies. A compromise possibility would be to reduce it to a warning.

@prolativ
Copy link
Contributor

In my understanding the colon is actually the key difference between a class/trait/object with a body and without it. Theoretically we could live without colon here at all and rely only on indentation in braceless syntax e.g.

trait Foo
trait Foo
  def bar = 1

but here it would be quite easy to miss the difference to

trait Foo
def bar = 1

A colon makes the difference more explicit

trait Foo:
  def bar = 1
trait Foo
def bar = 1

Allowing

trait Foo:
def bar = 1

with the meaning of the def outside of the trait would be even more confusing than getting rid of the colon at all.

@propensive
Copy link
Contributor Author

I wasn't proposing to remove the colon in all cases (and I think that was discussed at length a few months ago). But I'd agree that,

trait Foo:
def bar = 1

seems like it should really be at least a warning in all cases. In the absence of braces, the indentation shouldn't be optional. Too much flexibility around indentation is a bad thing.

@som-snytt
Copy link
Contributor

som-snytt commented Apr 22, 2021

You can use end. Not sure that is helpful for the use case.

The PSA slogan would go something like: "end isn't just for long things."

trait T:
// still not sure here
end T

Edit: "Not sure when pandemic will end?"

trait Pandemic:
end Pandemic

@odersky
Copy link
Contributor

odersky commented Apr 30, 2021

I think we do not have a clear understanding how to improve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants