Code aliases through header-style files #4662
Unanswered
TheWhiteGuardian
asked this question in
Language Ideas
Replies: 1 comment 3 replies
-
Your proposal implies that it would work as a general purpose macro but the example given seems more specialized. How would the compiler know that this |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Edit: ignore for now, see comments. This is pending a massive review because currently it is unsuitable for implementation.
One feature that I keep wishing C# would support is the ability to add code aliases: defining a specific keyword that, on compilation, would be replaced with a predefined bit of replacement code.
I've done some research as to why this isn't in C#, and found a few key problems that such a solution would have to solve, and I hope that my proposal does so pretty well.
Firstly, consider this bit of example code.
In this case, the OnEvent method is a handler for some event that gets called via reflection. The code analyzer doesn't know this though, and so it presents a warning message. We can disable this via an additional attribute:
However, this would require a lengthy attribute for each event handler. To solve this, I propose the addition of 'c# header files' if you will, which could look like this:
These files would serve as centralized definitions for code aliases, bound to a certain namespace just like types are, but present in separate, dedicated files such that they can be collected in a fast and straightforward manner.
The above file would then, in my proposal, add a definition for an alias (the icon I have in mind for these would be akin to an opera mask icon, which I can produce if necessary).
With the inclusion of this header file, the code can be simplified to this:
The benefits:
Accessibility modifiers would present a possible problem here: what prevents someone from using a restricted object through an alias? A possible solution would be to replace the aliases prior to intellisense analysis, which would then notice that an inaccessible object is being used.
I'd love to hear everyone's feedback on my idea. I absolutely love C# so I'm thrilled to have the opportunity to try and contribute to it.
Beta Was this translation helpful? Give feedback.
All reactions