Replies: 0 comments 2 replies
-
|
Do you have any examples where this would be useful? |
Beta Was this translation helpful? Give feedback.
1 reply
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.
-
💡 Problem
C# lacks flexibility in defining default method parameter values based on compile-time context. The current system, limited to literals or specific attributes, restricts language evolution and leads to boilerplate for advanced scenarios like context-aware defaults for generic types.
🌟 Proposed Solution
I propose adding a Roslyn extensibility point: an
IDefaultValueProviderinterface and aDefaultArgumentBindingContext. This allows developers to inject custom logic into the compiler's binding phase, providing rich, context-aware defaults and diagnostics for optional parameters.🧩 Public API
The following public types would be introduced:
1.
DefaultValueKindEnum2.
DefaultValueInfoStructStatic factory methods omitted for brevity.
3.
DefaultArgumentBindingContextstructAkin to other compiler extensions, a context is provided to access required data.
4. IDefaultValueProvider Interface
📦 How It Would Be Used
IDefaultValueProvider. The provider'sAppliesmethod determines if it's relevant for a given parameter, andProvideValuereturns the desired default.IDefaultValueProviderimplementations (as with source generators or analysers) and registers them with the C# compiler.Appliesmethod returnstruewill have itsProvideValuemethod invoked. TheDefaultValueInforeturned then guides the compiler in generating the final default argument expression.🏆 Benefits
This proposal offers a powerful and flexible way to extend the C# compiler, leveraging the strengths of Roslyn's extensibility model.
Beta Was this translation helpful? Give feedback.
All reactions