Why not extend do to allow unicode in ID's?

Dennis dkorpel at gmail.com
Sun Jun 30 10:10:41 UTC 2019


On Saturday, 29 June 2019 at 22:38:06 UTC, Bert wrote:
> Imagine being able to write proper mathematical formula ID's:

Currently D allows "universal alphas" in identifiers, so Greek 
letters are allowed already. See: 
https://dlang.org/spec/lex.html#identifiers

> or using valid mathematical operators:

In D you can't add operators, but if you want math notation on 
existing ones, you might be interested in fonts with programming 
ligatures such as:
https://github.com/tonsky/FiraCode

> or when you write a card game:

Custom literals can be added with templates, for example:
octal!377
(https://github.com/dlang/phobos/blob/d57be4690fc923a1974a4ef4d8b84a951131d219/std/conv.d#L4062)
tok!"if"
(https://github.com/dlang-community/libdparse/blob/5270739bcd1962418784c7760773e24d28b6009b/src/dparse/lexer.d#L115)

Since in strings any Unicode is allowed, you can do something 
similar:
suit!"♥"

> I don't write any code that anyone else reads so let me make 
> the choice for myself rather than create arbitrary rules that 
> limit expressiveness.

If it's only for yourself, you can add a build step that 
substitutes your custom symbols with valid identifiers before 
compiling. Or use your own fork of the compiler, you probably 
only need to remove this line:
https://github.com/dlang/dmd/blob/2599559d624275bfcff298b3a8b31f9d82ae534f/src/dmd/lexer.d#L524

Finally, if you truly long for ultimate freedom in how you write 
code, then Nim might be the right language for you since it 
aligns more with your "putting full trust in the programmer" view 
than D. In Nim, any non-ascii character is valid for identifiers, 
so even invalid Unicode characters are allowed.
https://nim-lang.org/docs/manual.html#lexical-analysis-identifiers-amp-keywords


More information about the Digitalmars-d mailing list