[Proposal] Declare attributes with a shorthand keyword #7739
Replies: 3 comments 7 replies
-
Adding this to the language doesn't enable anything that's impossible today. Attributes are used far more than they are defined. Simplifying the definition of attributes by adding something to the language will never pay for itself. See also minus 100 points: https://learn.microsoft.com/en-us/archive/blogs/ericgu/minus-100-points |
Beta Was this translation helpful? Give feedback.
-
It would have been awesome if you could do something like this through SG:
It's possible to do today but then you would have two types as opposed to a single one. |
Beta Was this translation helpful? Give feedback.
-
I agree that it's a question of ROI. Maybe a shorter syntax would be nicer here, but the vast majority of developers never write their own attributes so there is extremely little to gain. Attributes have existed in C# since the beginning and defining them has never been considered a pain point. Notable, the use of attributes in cases like this was done intentionally to avoid having to add new syntax to support every feature. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Attribute keyword
Summary
This proposal suggests introducing a new syntax for defining attributes using a dedicated keyword instead of inheriting from the
System.Attribute
class and appending theAttribute
suffix.Motivation
The motivation behind introducing a new attribute syntax is to enhance code expressiveness and developer experience. The current convention, requiring the inheritance of
System.Attribute
and appending theAttribute
suffix, can lead to verbose and somewhat boilerplate code. It can also cause confusion then the suffix is automatically removed when used, resulting in a discrepancy between declaration and utilization that can be counterintuitive, particularly for those unfamiliar with this convention. Introducing a dedicated keyword for attribute declaration aims to alleviate this confusion. By introducing a dedicated, it can streamline this process, providing a more concise and readable syntax.This change is not intended to replace the existing approach but rather as a more readable shorthand.
Detailed design
Current syntax
Proposal
Alternative proposal
Drawbacks
While the proposed syntax aims to enhance code readability, there are potential drawbacks to consider.
Learning Curve for Existing Developers: The introduction of a new attribute declaration syntax may pose a learning curve for developers already familiar with the traditional approach. Adapting to the new syntax might initially disrupt established coding habits and workflows.
Compatibility and Migration: Existing codebases heavily reliant on the current attribute syntax may face challenges during the transition. Ensuring backward compatibility and providing clear migration paths (like refactorings) would be crucial to minimizing disruptions.
Ecosystem Consistency: If widely adopted, the new syntax could lead to a mix of codebases using both the traditional and the proposed syntax. This inconsistency across the development ecosystem might create confusion and complicate collaboration.
Tooling Support: IDEs, analyzers, and other development tools will need updates to fully support the proposed syntax. The lack of immediate comprehensive tooling support could impede developers' ability to take full advantage of the new syntax.
Unresolved questions
The exact details of the syntax and potential corner cases need further exploration and may be refined during the prototyping phase.
Beta Was this translation helpful? Give feedback.
All reactions