ballerina-platform / ballerina-spec

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

Clarification on shadowing #873

Open chiranSachintha opened 3 years ago

chiranSachintha commented 3 years ago

Description: Do the following programs valid?

Program 1

function defaultClosure() {
    int k = 10;
    function(int k = k, int l = k + 2) mm;    // creates parameter with name `k`
}

Program 2

function basicClosure(int b) returns (function (int a) returns int) {
    int a = 3;
    var foo = function (int a) returns int {    // creates parameter with name `a`
        return a + b;
    };
    return foo;
}
jclark commented 3 years ago

The general philosophy is that the only kind of shadowing allowed is for a local variable to shadow a global variable. So I would say this should not be valid.

sanjiva commented 3 years ago

The first is invalid right??

Making the 2nd one disallowed would be annoying though.

jclark commented 3 years ago

They're both not valid, because they both have a local shadowing a local. I don't see any difference. (In the program 1, the k in the default for l refers to the k parameter.)