Non-ASCII characters in identifier

I’ve recently switched a project from 2.13.3 to 2.13.5. I’m not sure whether this is related because I’ve also updated (as suggested) to the newest scala-intelliJ plug-in, but I now get a warning about Non-ASCII characters in identifiers.

I’m using variables φ and λ, because I’m implementing a somewhat standard mathematical formula which most often uses those variables in its theoretical development.

QUESTION: Is there some danger of using non-ascii characters as identifiers?

I had to laugh when I saw the next non-ascii related warning:
Symbols from different languages, [LATIN,GREEK]. It’s puzzling why this should invoke a warning.

Screenshot 2021-02-24 at 10.21.06

1 Like
  1. AFAIK the JVM only supports ASCII characters for method names, thus those symbols or characters for other languages other than English have to be encoded. Which may make calling your code from Java (or another JVM language). But I may be wrong in this one, and too lazy to search.

  2. Not all code / text editors may be able to show those characters correctly.

So, in general, for code that is to be worked with and used by many people, I would believe that sticking to ASCII would be the best. But if the code is to be worked by and used by a small group of known people and all like the unicode characters then I would no see any real problem.

Hi,
I haven’t tried it myself, but you could specify UTF-8 as the default character set, e.g. as a scalacOption. This permits non-ascii characters to be used.

I have many times translated math and science formulas into code throughout my career, and typically, if the formula has an Greek letter, I just spell out the name of that letter in English (phi, lambda) etc.

The reason against using non-English letters is not even Java interop. As far as I can tell, Greek letters are just letters and perfectly valid in identifiers in the Java language as well as on the JVM side. So, that is even smoother than handling operator symbols, which are valid in Scala operators, but need to be escaped when compiled (e.g. + becomes $plus etc).

The concerns with non-English letters is, first of all, unless you have a Greek keyboard, it is non-trivial to input a Greek letter.

Second, where do you stop? Are you going to allow only English and Greek, or any alphabet? Soon, you will be unable to identify letters. For example, can you tell these letters: Са́харов.

2 Likes

On a similar note, what is the deal with single quote as an identifier character? I guess there was discussion of this early on and the decision was against it. But in functional it is very common to allow x, x’, x’’, x’’’ etc as identifiers.

The question of what kind of advice a linter should warn about is a never ending discussion, I know. Where does advice stop?

I once write a linter for a lisp dialect we were using, and I though my warnings were completely reasonable, but the caused lots of arguments. For example, I demanded that unused variables either be declared as unused, or begin with an underscore. I eventually had to abandon this basically because people didn’t want unused variables checked. :frowning:

On a similar note, what is the deal with single quote as an identifier character?

You can do this. The character you want is “modifier letter prime” U+02B9.

@ val a = 1 
a: Int = 1

@ val aʹ = a + 1 
aʹ: Int = 2

If you use a Mac you can add it to your favorites on the Special Chars palette so it’s just a few keystrokes away.

As for non-Roman characters, that’s a problem with your IDE. It’s totally fine with Scala. We have been using this code for years. Similar to what you’re doing, looks like :wink:

3 Likes

sorry, what is the Special Chars palette? I didn’t know I had one.

sorry, what is the Special Chars palette ? I didn’t know I had one.

Yes, hit Ctrl+Cmd+Space while you are typing and it will pop up. It may give you the full special characters palette, in which case you can click the button in the upper-right to shrink it. Then close it and try again and it should pop up above or below where you’re typing. You can use arrow keys to navigate and Return to insert the selected character, so you can do it all without touching the mouse.

To add to your favorites, expand it to the big view and the detail of the character on the right will have an “add to favorites” button.

2 Likes

apparently Cntl+Cmd+Space does not work in IntelliJ.
When I press that in IntelliJ, nothing happens. But if I type it in a unix shell, I see the popup. :frowning: