Source Generator to Create Concrete Constant<T> Subclasses #191
zacharylayne
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
|
Currently, I'm not investing time in this feature. .NET 8 is coming, and starting from November I'll be focused on releasing next major version of .NEXT 5.x. Since .NET 7 (and even .NET 6, with enabled preview features) constant generic argument can be efficiently replaced with static abstract interface property, e.g.: public interface IConstant<out T>
{
static abstract T Value { get; }
}
static void MethodWithGenericConstant<T>() where T : IConstant<int>
{
int value = T.Value;
}Therefore, I have a plan to remove constant generic arguments in the next version. |
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.
-
Really loving the constant generic arguments, but it feels like busy work to define
Constant<T>subclasses. I'd like to suggest creating a source generator to do that instead.My idea is that usage would be similar to the following code:
The
ConstantAttribute<T>must be applied to a partial abstract class that derives fromConstant<T>. This would create nested subclasses with their names & constant values specified along with the attribute. The attribute would have a static constructor to validate thatTvalues can be constant, and a protected instance constructor to validate class names.Feel free to let me know if this is a dumb idea! :|
Beta Was this translation helpful? Give feedback.
All reactions