-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Struct constructor doesn't require initialization of class reference #21
Comments
Yes, this is ... kind of hard to fix. The problem is that we'd need control flow analysis (or runtime errors) to catch cases where there exist flows through the constructor that don't set a field. At the limit, this is no-shit halting-problem tier undecidable. I could hack something like "every field in the struct must have at least one assignment somewhere in the constructor", but I fear this would only give the false impression of safety. |
Yes, some kind of basic flow analysis would be needed. If implemented like cppfront, the following code would error: this(){
if (random!bool()) // line 12
c = new C;
}
Requiring |
Okay, I'm gonna say that yes, I could do that, but I'm not gonna in the short and medium term. It seems a lot of effort for a mid improvement, and a lot of compiler machinery that wouldn't be used anywhere else. (Neat is relatively less reliant on compiler analysis than some other languages.) Patches, as usual, welcome! |
For now perhaps an out contract for structs with non-nullable members could be added in constructors that asserts that those members are not null. Later the
This would also be useful to ensure a struct with both a defined constructor and destructor is actually constructed and not left as T.init. This would guarantee that such structs do not have to handle T.init in their destructor (which has a runtime cost as well as a design cost to detect the init value). Then if there is no initialization of the struct, the compiler can avoid calling the destructor or allowing it to be copied. (I also raised this here).
Thanks, I haven't looked at the source yet. (I'm a bit put off by the compile time as I'm spoilt by dmd ;-)). |
Initial compile time is high, but follow-up should be fine as the cache is warm.
That's a good idea with the out-condition assert! I'll put it on the TODO list. Note that I'm distracted ATM making a toy cpu in VHDL. |
Declaring
S s;
gives:But declaring
S.this
without initialization ofc
is not caught.s.c
is thennull
.The text was updated successfully, but these errors were encountered: