nukie nukie - 24 days ago 5
Java Question

line breaks after Java annotations in Scala code

I have a code that compiles successfully:

import javax.ws.rs.Path

trait DescriptionHandler extends ServiceAwareHandler {
@Path("/descriptions")
def getDescriptionsRoute: Route = ...
}


and it would not compile if I add a line break after Path annotation like this:

import javax.ws.rs.Path

trait DescriptionHandler extends ServiceAwareHandler {
@Path("/descriptions")

def getDescriptionsRoute: Route = ...
}


In IntelliJ IDEA it look like javax.ws.rs.Path is absent in class path.
Compiler telling me this:

Error:(19, 1) expected start of definition
def getDescriptionsRoute: Route =


What's the problem with line breaks between Java annotations and Scala methods / fields? I tried to google the issue but didn't found anything useful.

Answer

In Scala Spec/Lexical syntax/1.2 Newline characters there is an explicit statement about this case:

A single new line token is accepted

  • ...
  • after an annotation.

And when there is an empty line:

if two tokens are separated by at least one completely blank line (i.e a line which contains no printable characters), then two nl tokens are inserted.

Thus blank lines are not accepted between annotation and definition.