Proposal: Introduce meta modifier to append attribute data for source generators #9025
Unanswered
X39
asked this question in
Language Ideas
Replies: 0 comments
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.
-
1. Summary
Currently, source generators cannot declare attributes on methods to achieve functionality similar to
#nullable
. This limitation prevents certain scenarios from being addressed at a community level.This proposal seeks to address this issue by enabling more complex scenarios through a new language feature.
1.1 Table of Contents
1.1. Table of Contents
5.1. Allowing multiple partial declarations
5.2. Don't change anything
6.1. CheckedExceptions
6.2. API-Controller and
ProducesResponseTypeAttribute
6.3. 6.3. Copying documentation into attributes
2. Motivation
I am currently considering creating
CheckedExceptions
andTypeAlias
analyzers. For both of these, I realized that auto-generated attributes are essential to make them truly effective.At present, users are required to manually create these attributes, which is far from ideal due to the significant amount of additional boilerplate it imposes. There are also other scenarios from my past work where this feature would have been highly beneficial.
Due to the immutability constraints of the context in which source generators operate, I propose introducing this new language feature to address these limitations.
3. Proposed syntax
(will try to keep this updated)
4. Detail
I propose the addition of a new
meta
modifier, which can be applied to all type-defining constructs (e.g.,class
,struct
,enum
,interface
, etc.) to specify additional metadata. Themeta
modifier would follow these rules:where
clauses on generics).partial
) should be required to use meta types.AllowMultiple = false
, an error is raised as if the conflict occurred in the concrete type.AllowMultiple = true
, no error is raised, and all attributes are added.A meta type can be conceptualized as a "partial" of its corresponding concrete type, with each meta type contributing attributes to the concrete type.
5. Alternatives
5.1. Allowing multiple partial declarations
An alternative approach could be to allow multiple partial declarations to coexist. Currently, this is not permitted because the compiler raises a
Member with the same signature is already declared
error.However, this approach has several downsides:
partial
, which adds additional overhead and complexity for users.5.2. Don't change anything
Not changing anything, obviously, has the benefit of no changes being required. The user will have the burden of adding attributes to the correct place and source generators will continue to have limited attributation capabilities.
6. Examples
The following sub-chapters provide examples that have been simplified as much as possible. Please focus on the concepts being illustrated rather than critiquing the code itself, and consider the examples as presented for clarity.
6.1. CheckedExceptions
Consider the following, very simple example:
The sheer amount of boilerplate required quickly becomes overwhelming and difficult to manage in source code. Expecting users to manually add all of this is not only tedious but also makes the code harder to read and maintain.
Meta types significantly reduce this burden by enabling the source generator to handle the heavy lifting automatically. Users are freed from the complexity, needing only to add the marker attribute to leverage this functionality effortlessly.
6.2. API-Controller and
ProducesResponseTypeAttribute
Consider the following, very simple example:
The meta class facilitates automated type annotation for API controllers, significantly reducing the likelihood of human errors when annotating data that code depends on.
For instance, ProducesResponseType attributes can be automatically generated, ensuring that accurate and consistent OpenAPI documentation is produced in all cases. This process is seamlessly supported by a source generator in this scenario, streamlining the development workflow.
6.3. Copying documentation into attributes
In some cases, it is necessary to extract documentation comments and embed them into attributes for later use via reflection.
This proposed language feature would enable source generators to accomplish this efficiently, streamlining the process and reducing manual effort.
Beta Was this translation helpful? Give feedback.
All reactions