[Proposal] Only allow Lexical Contextual Keywords in the Language #4458
-
Only Allow Lexical Keywords in the Language
SummaryToday there are keywords in the language that cannot be understood with just lexical information such as MotivationIn general, C# strives to be a language that is explicit about program behavior and normally required the developer to write out what their intention is without ambiguities. This makes is a language that is easy for someone to read and understand once you learn it as there is little implicit behavior. I believe having this "gotcha" where keywords are only keywords if nothing else in scope is so-named makes the language harder to read and reason about in general. There is also the reality that the design goals around C# Language versions and .NET Framework versions have changed. In the past it was paramount that developers could take a new language version update without updating the framework version they were targeting. With language features being increasingly tied to the runtime this makes less sense. We now strongly encourage developers to update both the language version and target framework together. Detailed designThere is an existing concept in the language called "contextual keywords". I am not proposing doing away with this concept altogether just changing it so that a keywords "contextual-ness" is always able to be determined lexically. Take new (at the time of this writing) keyword
Similarly
The implementation of this proposal would remove wording from the spec around name lookup collisions, and have a compliant compiler be able to fully determine keywords given only parsing information. The following keywords would now error if developers attempted to use them as anything other than a keyword
DrawbacksThis is a breaking change, if anyone was relying on this behavior in their code it would no longer compile. For most cases where a type named AlternativesWe could opt to keep |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 15 replies
-
I feel like it's worth mentioning that the "migration path" for most cases where a type named |
Beta Was this translation helpful? Give feedback.
-
I believe that @jaredpar's position on this is that we could tie this to language version upgrades, similar to the breaking change we took for 'record' parsing. If we do this, then we can take this codepaths out for later versions, and effectively limit support for previous versions. |
Beta Was this translation helpful? Give feedback.
-
I woudl be surprised if any formatting tooling actually cared about this (including for the nameof case). In practice So i don't see this really buying much at the tooling level TBH, as tehse tools shoudl care about the practical case, not the pathological. |
Beta Was this translation helpful? Give feedback.
-
I've moves this proposal to an issue here |
Beta Was this translation helpful? Give feedback.
I've moves this proposal to an issue here