Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Scala separate symbols and letters in identifiers, and can I change this?

Tags:

scala

I'm thinking of using Scala for an internal DSL (ie, the DSL is really Scala) for musical composition. I'd want to use identifiers with characters like the sharp sign, ♯ (U+266F). Looks like this is not possible:

val c♯0 = 13

does not parse. The closest I can get is:

val c0_♯ = 13

I don't like it. What's the rationale for forcing identifiers to be named like this?

like image 490
Rob N Avatar asked Sep 15 '25 00:09

Rob N


1 Answers

Scala's grammar tries to cater to a lot of conflicting requirements. In order to make optional any white-space between "operator-like" method names (such as +, / or ::) and their arguments on either side, it's necessary to have a way of signifying that a given name contains a mixture of punctuation and alphanumeric characters.

Keep in mind that internal DSLs are not new languages! They're at best illusions of being new languages. They are still Scala.

If you want complete freedom to control the language you provide to your users, you can write a combinator parser or use an external parser generator.

For anyone involved in language processing in Scala, I highly recommend you check out Kiama before rolling your own narrowly focused, single-purpose language processing code. Kiama is a gem!

like image 73
Randall Schulz Avatar answered Sep 17 '25 20:09

Randall Schulz