Before anything else is said, def does not define a field; it defines a method.
In the second case, you can omit the parentheses due to the specific Scala function. There are two distinctive features here: one mechanical and one of the recommended applications.
Starting with the latter, it is recommended to use an empty parameter list when there are side effects. One classic example is close() . You would not mark parentheses if there are no side effects to invoke the item.
Now, as a practical difference β beyond the potential for strange syntactic confusion in angular cases (I'm not saying that it is, just a hypothesis) βstructure types must follow the correct convention.
For example, Source had a close method without parentheses, which means that the structural type def close(): Unit will not accept Source . Similarly, if I define a structural method as def close: Unit , then Java closeable objects will not be accepted.
Daniel C. Sobral
source share