Create an attribute that, when applied to a struct type, indicates that the default value of that struct is invalid #70600
Replies: 1 comment
-
Closing out. This is already possible today with analyzers. Just define that attribute, and write an analzyer that does whatever detection you want.
The benefit of doing your own analysis is that you can define exactly what the 'obvious cases' are and expand on htat later, especially with domain knowledge. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
With the latest versions of C# it is possible to override the default ctor for struct types which is useful to help prevent badly-formed struct-type values, but does not prevent usage of the default(T) value of a struct-type (either explicity or implicitly e.g. as part of an array or non-initialized class member).
Although the compiler can never fully prevent someone from creating a value of default(T), having an attribute we can apply to struct types that gives a hint would be useful. In these cases, where the compiler detects that a value will be default(T) for a type T that is annotated with this proposed attribute, it can emit a warning.
The compiler would only have to detect the most obvious cases-- but these would still be very useful in minimizing errors.
Examples always help, so here's an example:
Beta Was this translation helpful? Give feedback.
All reactions