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
IDefaultValueProvider
interface 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.
DefaultValueKind
Enum2.
DefaultValueInfo
StructStatic factory methods omitted for brevity.
3.
DefaultArgumentBindingContext
structAkin to other compiler extensions, a context is provided to access required data.
4. IDefaultValueProvider Interface
📦 How It Would Be Used
IDefaultValueProvider
. The provider'sApplies
method determines if it's relevant for a given parameter, andProvideValue
returns the desired default.IDefaultValueProvider
implementations (as with source generators or analysers) and registers them with the C# compiler.Applies
method returnstrue
will have itsProvideValue
method invoked. TheDefaultValueInfo
returned 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