Skip to content

Unable to define an empty trait with indentation-based syntax #10080

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
julienrf opened this issue Oct 25, 2020 · 3 comments
Closed

Unable to define an empty trait with indentation-based syntax #10080

julienrf opened this issue Oct 25, 2020 · 3 comments

Comments

@julienrf
Copy link
Contributor

Minimized code

trait Foo:
end Foo

Output

This code does not compile. The error message is:

end Foo
^^^
indented definitions expected

However, it is possible to create an empty trait with the brace-significant syntax:

trait Foo {
}

Expectation

The first definition of trait Foo should compile just like the last one.

@odersky
Copy link
Contributor

odersky commented Oct 27, 2020

In fact, you can define an empty class with an end marker:

trait Foo
end Foo

But it does not work anymore if you add a :.

@julienrf
Copy link
Contributor Author

I think the following should be valid:

trait Foo
trait Foo:
// Equivalent to
trait Foo {}
trait Foo:
end Foo
// Equivalent to
trait Foo {
}

But the following should be rejected:

trait Foo
end Foo
// Equivalent to
trait Foo
}

bishabosha added a commit that referenced this issue Oct 28, 2020
Fix #10080: Allow end marker immediately after `:` at eol
@som-snytt
Copy link
Contributor

I was about to contribute a reversion when I noticed the behavior was intentional.

This violates the principle that end marker is optional and can be removed without breaking compilation. End markers are not braces.

#12340 (comment)

end markers an optional addition to a program. The program needs to be syntactically correct even if they are missing.

#12918 (comment)

Related judgment

In my understanding the colon is actually the key difference between a class/trait/object with a body and without it.

#12185

Notice that there are a couple of disappointed expectations in the previous comment #10080 (comment)

trait T:   // not ok without body

trait T
end T   // perfectly fine

The misapprehension at #12340 is that there is something wrong with

trait T {
}
end T

where I will contribute my diabolical suggestion

trait T:
  def t = ???
end T
// unindents to
trait T {
  def t = ???
} // end T

because if it needs an end marker, then it also needs an end comment.

Currently, -rewrite -no-indent does not modify

class C:
end C

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

4 participants