Replies: 6 comments 13 replies
-
What about users who have been carefully using |
Beta Was this translation helpful? Give feedback.
-
I prefer option 2 plus a warning if a type with a no-arg constructor is initialized via |
Beta Was this translation helpful? Give feedback.
-
And it has always meant to invoke a parameterless constructor, if one exists, otherwise zero out the struct. Granted the odds of running into such a beast was probably quite rare given the lack of support for it in the major MS .NET compilers, and I imagine that it will continue to be rare even after the introduction of this feature. I'd rather the compiler emit the default parameterless constructor for the initializer here. I feel that doing anything else will be more surprising and lead to more bugs. Otherwise, if the team goes with not emitting a default parameterless constructor, I would leave instance field and property initializers illegal. |
Beta Was this translation helpful? Give feedback.
-
While I think resolving this gap in the language would be nice, as the notes imply, none of the 3 possibilities comes across as "obviously" better. Some users are likely to be surprised no matter what option is chosen. Given that, I think doing nothing might be the best option. Also, I don't really understand what the motivation for doing this proposal now are. The motivation in the proposal document is entirely technical and has no business use-cases listed (performance benefits, scenarios currently blocked, etc). This seems like a minor annoyance that we've lived with for 20 years already and its not worth making potentially confusing changes to try and fix it. |
Beta Was this translation helpful? Give feedback.
-
So, the ideal behavior is 1, while the others are only for the reason of backward compatibility in case the struct has instance field initializers. Why not do 1 and issue an warning in case it has any instance field initializer? |
Beta Was this translation helpful? Give feedback.
-
I figure this would be a good place to resurface an idea that some of us raised a while ago: non-zero default structs. I feel that the reason that all three of the options for parameterless struct constructors are less than ideal are because none of them address the underlying issue that sometimes we want the default value of a struct to be something other than zero. I'm not sure if "blitting" is the right term here (I picked the term up up from the "Blitter" coprocessor in the Commodore Amiga's Agnus chip) but as far as I know, copying a value from one memory location to another is not significantly slower than zero-filling that location. In fact, I suspect it gets very chip-specific in terms of the speed of moving values from registers versus caches as I'd be surprised if there was any measurable difference in real-world scenarios. It therefore seems sensible to me that the team investigate having the compiler precompute the default values of structs and have It may not work, but I'm not aware that its ever been investigated, so we don't know that. And if it is viable, then the issue of how to handle |
Beta Was this translation helpful? Give feedback.
-
https://github.com/dotnet/csharplang/blob/main/meetings/2021/LDM-2021-03-29.md
Agenda
Beta Was this translation helpful? Give feedback.
All reactions