dotnet / csharplang

The official repo for the design of the C# programming language
10.95k stars 999 forks source link

[Proposal]: Extended identifier syntax #7965

Closed leppie closed 2 months ago

leppie commented 2 months ago

Extended identifier syntax

Summary

Allow non-standard identifier syntax in C#.

Motivation

Allow C# to reference any allowable CLR identifier in assemblies produced by non-C# languages for interop purposes.

Detailed design

Some languages do not conform to the C# identifier syntax, but still are valid within the CLR specification.

This proposal introduces a (lexical) syntax extension that is compatable with the C# lexical specification and also allow other language names to be referenced without having to resort to reflection or other means (ie post-processing or raw IL).

In the spirit of Markdown, I suggest the ability to escape any identifier between 2 backquotes.

Examples:

`a-b`
`+`  
`$/bitcoin`

The resulting text between the two backquotes will exactly reference (and resolve, when used) a single valid CLR identifier.

Usage (using and calling a static method);

using `F#/C#`;
`$bob`.`an-alice!`(typeof(`@terrajobst`));

Justification

Shameless plug: https://github.com/IronScheme (but specifically IronScheme.Glue.il needed for interop).

Drawbacks

It is ugly, but it is ugly for a reason. General usage should not be encouraged.

Potential parsing issues with comments, preprocessor directives and other existing lexical tokens.

Alternatives

There are no static alternatives except raw IL or post-processing like Fody.

Unresolved questions

To discourage usage even more, a compiler flag/property could/should be added.

Design meetings