3 ms·
Callee: creates a complex object and does several steps of initialisation. Last step fails, so it returns a partially initialized object and an error. Caller:
by usrnm 3y ago
Callee: creates a complex object and does several steps of initialisation. Last step fails, so it returns a partially initialized object and an error.
Caller: checks the error, which is not fatal, but expects to get a nil in this case. Ends up with a non-nil partially initialized garbage.
Yes, someone screwed up. Yes, it's a bug. Doesn't change the fact that there are better error handling approaches that eliminate this kind of bugs completely
- nordsieck 3y agoYou're right that the language doesn't protect you here. One easy way to prevent these sorts of errors is to always return default literals with errors (in the same spirit of "value == var" from c to prevent accidental assignment).