Search code examples
scalavariablesoperatorsunary-operator

How Scala distinguishes between unary operators and characters in variable name


It seems that in Scala, a variable name can end with specific character, such as -.

How are those trailing characters distinguished from the postfix unary operators during parsing?


Solution

  • Those characters must be joined to the identifier with an underscore.

    For example, a! parses as a.!, whereas a_! parses as a single identifier.