ballerina-platform / ballerina-spec

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

Should the spec say `self` is implicitly `final`? #1235

Closed MaryamZi closed 1 year ago

MaryamZi commented 1 year ago

Description: $title.

Came up in a query related to isolated, where we try to use self as a captured variable in an isolated anonymous function within a method of a readonly class.

readonly class Test {
    string[] words;
    int length;

    isolated function init(string[] & readonly words, int length) {
        self.words = words;
        self.length = length;
    }

    isolated function getCount() returns int {
        var fn = isolated function(string word) returns boolean {
            // ERROR: invalid access of `self` 
            return word.length() == self.length;            
        };
        return self.words.filter(fn).length();
    }
}

The jBallerina implementation also doesn't seem to log a compilation error when trying to assign a value to self at the moment.

readonly class Class {
    int i;

    function init(int i) {
        self.i = i;
    }

    function fn() {
        // no compilation error on jBallerina
        self = new (3);
    }
}
jclark commented 1 year ago

Yes, I think self should be treated as implicitly final, like a parameter.