ballerina-platform / ballerina-spec

Ballerina Language and Platform Specifications
Other
168 stars 53 forks source link

Behaviour on attempting to update a potentially `readonly` record field #495

Closed MaryamZi closed 4 years ago

MaryamZi commented 4 years ago

Description: Consider a field update via a union of two records, where both have a field by the same name, but in one record type-descriptor it is a readonly field.

type Student record {|
    readonly string name;
|};

type Employee record {|
    string name;
|};

public function main() {
    Student|Employee rec = <Student> {
        name: "May"
    }; 

    rec.name = "Jo";
}

IMO, we can allow this at compile-time, and if rec is actually a Student, we can panic at runtime with an inherent type violation error.

Or should we disallow this at compile time?

hasithaa commented 4 years ago

I think we can live the first; panic at runtime. @jclark thoughts?

jclark commented 4 years ago

It should be a runtime panic and not a compile-time error.

Consider the Employee record. Absence of readonly on a field does not mean writable, rather it means "may or may not be readonly". Since it is not a compile-time error to write to the name field of something declared as Employee, it should not be a compile-time error to write to the name field of rec.