3 ms·
> Pro tip for language designers: require all fields to be initialized any time an object is created. This proposal sounds great until you find out that this i
by turminal 5y ago
> Pro tip for language designers: require all fields to be initialized any time an object is created.
This proposal sounds great until you find out that this is a hard problem to solve reasonably well in the compiler and no matter what you do there will be valid programs that your compiler will reject.
- pjc50 5y agoNo? This seems to work perfectly fine for other languages. > valid programs that your compiler will reject. Do you mean "valid programs where everything is initialized when an object is created will somehow fail to detect that"?
- Arnavion 5y ago>Do you mean "valid programs where everything is initialized when an object is created will somehow fail to detect that"? Valid programs where a field is left uninitialized at creation time, but the programmer makes sure it's initialized before it's used.
- pjc50 5y agoSure, so we can easily change the definition of "valid" so that you have to initialize them at creation time. shrug
- db48x 5y agoNo, I don’t think that this should be considered a valid program. I would never allow any code that creates a partly–initialized struct in any code that I maintain. I agree that it’s a hard problem, but I don’t think that language designers should use that as an excuse. I think that language designers should really double–down and require that every field be initialized, otherwise people will just forget. The code will be carefully written at first, but then someone new will come along and add a new field, and then the existing code is insufficient. With no errors from the compiler there is no way to ensure that the new programmer updates everything to accommodate the new field. Rust does pretty well in this regard, but you can still use unsafe blocks to get a partly–initialized struct if you really want one. I like Rust, but I wish that it went all the way and didn’t allow it at all.