D.L.
D.L.

Reputation: 169

Naming convention for scala definitions

I am trying to have a definition of the name +I in scala as follows.

def +I(i1: Int, i2: Int): Int = { .....}

It says '=' expected but identifier found. If I do I_+, it works, but for ease of use, I wanted to have +I. Is it not possible to have this name for a definition?

Upvotes: 1

Views: 81

Answers (1)

Jakub Zalas
Jakub Zalas

Reputation: 36191

It is possible to use such a name for a definition, but it won't look nice. You need to put the name between the backticks:

def `+I`(i1: Int, i2: Int): Int = 42

Usage:

`+I`(1, 2)

Lexical Syntax for Scala is summarised in https://www.scala-lang.org/files/archive/spec/2.11/13-syntax-summary.html

Here are the most relevant details:

whiteSpace       ::=  ‘\u0020’ | ‘\u0009’ | ‘\u000D’ | ‘\u000A’
upper            ::=  ‘A’ | … | ‘Z’ | ‘$’ | ‘_’  // and Unicode category Lu
lower            ::=  ‘a’ | … | ‘z’ // and Unicode category Ll
letter           ::=  upper | lower // and Unicode categories Lo, Lt, Nl
digit            ::=  ‘0’ | … | ‘9’
paren            ::=  ‘(’ | ‘)’ | ‘[’ | ‘]’ | ‘{’ | ‘}’
delim            ::=  ‘`’ | ‘'’ | ‘"’ | ‘.’ | ‘;’ | ‘,’
opchar           ::= // printableChar not matched by (whiteSpace | upper | lower |
                     // letter | digit | paren | delim | opchar | Unicode_Sm | Unicode_So)
printableChar    ::= // all characters in [\u0020, \u007F] inclusive
charEscapeSeq    ::= ‘\‘ (‘b‘ | ‘t‘ | ‘n‘ | ‘f‘ | ‘r‘ | ‘"‘ | ‘'‘ | ‘\‘)
op               ::=  opchar {opchar}
varid            ::=  lower idrest
plainid          ::=  upper idrest
                 |  varid
                 |  op
id               ::=  plainid
                 |  ‘`’ stringLiteral ‘`’
idrest           ::=  {letter | digit} [‘_’ op]

FunDcl            ::=  FunSig [‘:’ Type]
FunSig            ::=  id [FunTypeParamClause] ParamClauses

Upvotes: 5

Related Questions