-
Notifications
You must be signed in to change notification settings - Fork 755
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
Overriding a field included with record-type-inclusion doesn't remove the default value closure for that particular field being overriden #32012
Comments
The culprit for this issue is when we initialize R1, it automatically calling the init of referenced types. import ballerina/io;
type R record {|
int hello = inc();
|};
type RR record {|
*R;
int hello = inc();
|};
public function main() {
R|RR rr = <RR>{ hello: 22};
lock {
io:println(i); // prints 2 [`inc` method have invoked twise, even when value for `hello` is given to the constructor]
}
}
isolated int i = 0;
isolated function inc() returns int {
lock {
i = i + 1;
return i;
}
} |
We desugar the initialization logic of all the fields into the generated init method of the record. This initializes all of the fields in the records for which default values have been specified. With type inclusion, I believe including records call the init methods of the included records to initialize fields that are initialized in the included records. But this'll result in errors for examples like in the issue. Just removing the init call of the included record isn't an option because for some fields the default value will still come from the included record. type R0 record {
int|string f = 1;
int? g = 1;
};
type R1 record {
*R0;
string f;
// g is initialized in R0
}; To properly support inclusion like in the example IMO we need to be able to selectively call the initialization logic for specific fields. We may need to introduce the concept of default value closures attached to each field. |
ATM calling the included record types in it calls generated during codegen. We can set a skipped field set during included record initialization but might cause more issues if we try to do that at runtime. Perfect solution would be to have default value closure for each field as @MaryamZi said. So we can independently initialize each record. |
This issue is NOT closed with a proper Reason/ label. Make sure to add proper reason label before closing. Please add or leave a comment with the proper reason label now. |
Description:
This results in:
Steps to reproduce:
Affected Versions:
OS, DB, other environment details and versions:
Related Issues (optional):
Suggested Labels (optional):
Suggested Assignees (optional):
The text was updated successfully, but these errors were encountered: