3 ms·
> No statements go by where the object is in an intermediate, invalid state. This seems kind of misleading. Per my understanding of Go, omitted fields in a st
by dllthomas 11y ago
> No statements go by where the object is in an intermediate, invalid state.
This seems kind of misleading. Per my understanding of Go, omitted fields in a struct initialization are defaulted not reported as errors. So you're equally likely to be passing invalid state, in the two situations.
One pattern I like in Haskell, for this kind of thing, is to define a defaultConfig value that contains typical defaults and which can then be tweaked as desired.
One big advantage this has is that when some functionality is made newly configurable, you set the default to be the old behavior and existing code continues to work correctly unmodified without any additional effort.
- sagichmal 11y agoHopefully you'll combine that idiom with "make the zero value useful" so that omitted fields still give valid behavior.
- dllthomas 11y agoReading on, I see that. It still breaks down when a zero value is meaningful for a configuration parameter, but means something different than what was done before the parameter was configurable. I think that's probably not super often. In light of all this, I still don't really see what harm we're avoiding by preferring the inlined struct initialization. I do agree it looks a little prettier, but the article seems to give it a greater import.