Replies: 2 comments 5 replies
-
Will this work if we had Logging frameworks could definitely take advantage of that so there wouldn't be a need for runtime parsing of message templates. |
Beta Was this translation helpful? Give feedback.
-
This was discussed a little on Discord but I'll post my findings here as well. TL;DR: For a struct with field initializers and without a parameterless constructor, emit a private parameterless constructor. Will handle the warning/error on Per the design notes the plan is to not synthesize a parameterless constructor for a struct that has field initializers. This will mean that the current behavior of This approach has a very large blast radius, since it applies to a lot of structs, and the developer must opt-in otherwise they would not have any indication that field initializers might not be executing. This approach would certainly make sense if the eventual goal is to make Since these structs with field initializers would be a brand new beast in C# 10.0 I suggested the use of some metadata on the struct which would denote that these structs have field initializers. This would allow the compiler to more narrowly focus the compiler diagnostic to only apply to new structs, which would also mean that it would not have to be a warning wave. It could even be a compiler error, further forcing the developer to explicitly decide between So I suggested that the metadata applied to these structs actually be a synthesized private parameterless constructor. In theory the C# compiler already supports consuming structs with parameterless constructors so it should also already prevent using Unfortunately, in testing I found that this is not the case. The C# compiler will happily allow you to use I would imagine that both of these issues will be dealt with as a part of implementing parameterless ctors in structs. As such, what I propose is that if a struct is declared with field initializers and does not declare a parameterless constructor that the compiler emits a private parameterless constructor. This constructor would serve as the metadata to indicate that it is not safe to create the struct with Another advantage of emitting a private parameterless constructor is that it helps to close the generic hole. The runtime is already doing its part and such a struct cannot be used with a generic method that has either the Thoughts? |
Beta Was this translation helpful? Give feedback.
-
https://github.com/dotnet/csharplang/blob/main/meetings/2021/LDM-2021-04-28.md
Agenda
Beta Was this translation helpful? Give feedback.
All reactions