Variable scoping seems overly restrictive

I've been trying to get a bunch of open source projects to compile, and a lot of the older ones have issues like:

function foo(bar) {
    do_something(bar);
    var bar = 0;
    ...
}

which results in "ERROR: Redefinition of variable 'bar'.

At first I thought the projects were just broken... but this is such a common pattern, across lots of different projects, that I'm assuming the behavior here has changed, and this used to compile successfully. Is there a hidden flag that would let it compile again?

I was also surprised to get errors for code like:

function foo(a) {
    var x = 0;
    if (a) {
        for (var x = 0; x < 10; x++) {
            // ...
        }
    }
}

While such code is asking for trouble, I'd expect more of a warning than an error.

Finally I found one interesting case thats sort of a combo:

function foo(a, i) {
    for (var i = 0; i < a.size(); i++) {
        // do something
    }
    // a is an array
    a.add(i);
}

Since I'd been mechanically fixing a few of these, my initial fix was to drop the "var" on the i in the for loop, but then we end up adding a number to the end of the array, when i was in fact a string. Doing this broke the app in such an obvious way that I have to think that at one time, this actually "worked". The i in the for loop was scoped to the for loop, and a.add(i) added the original value of i to the array.

Can anyone comment on whether the behavior really has changed? And if there are flags to get the old behavior back?

  • Sure, but you posted MonkeyC code, not pseudocode.

  • thanks for that insight

  • Not sure this is an "optional error" any more as the change was made sometime back (months) likely to fix another error (you may find it in the change logs that are in the README file that come with every SDK). 

    If stuff on github hasn't been changed to fix it, that's the responsibility of whoever posted that code or downloads that code, not garmin.  Just another reason to be careful with any source code you find on github.

  • that's the responsibility of whoever posted that code or downloads that code, not garmin

    While thats true, it's generally a good idea not to make gratuitous breaking changes. Making this a warning, or an error *by default* with an option to suppress the error would have been just as effective...

  • This was a minor change, one that was easy to detect and fix.  One that happened many months ago.

    As I've said, this fix was very likely to fix another issue (Monkey Types is possible) and having an option to ignore the error really isn't an option.

    BTW, this is far from the first time a change like this happened in the 7 years monkey C has been around.

  • I'm wondering about the doc section "Switch Block Variable Scoping":

    developer.garmin.com/.../

    As is described there a switch block allows "Variables defined within curly braces of a case block will be scoped at that code block level." And for case "B" also the comment in the code line "var aaa = true; // Scoped at the code block level within the curly braces, no scoping conflict with variable aaa at the swtich block level"

    switch ( obj ) {
        case true:
        var aaa = 1; // Scoped at the switch block level
        ...
        case 1:
        var zzz = aaa; // Results in a compiler error because aaa was not initialized in this case block
        ...
        break;
        case "B": {
           var aaa = true; // Scoped at the code block level within the curly braces, no scoping conflict with variable aaa at the swtich block level
           ...
           break;
        }
        case instanceof MyClass:
        var aaa = "Hello!" // Results in a compiler error because aaa has already been defined in the switch block
        ...
        default:
        aaa = 0; // aaa was defined in the first case and initialized at the beginning of the default case, no errors!
        var good = aaa;
        ...
        break;
    }

    But now for this case the compiler SDK 4.1.1  terminates with an error because of redefining var aaa. Obviously the compiler has changed but the doc doesn't.

    [EDIT]: And case 1 doesn't result in a compiler error anymore.

  • All the Garmin examples I have tried (https://github.com/garmin/connectiq-apps/) give error now, I try to fix them but sometimes I get kind of blocked.


    How can I modify downloaded barrels? If I try to access that path in my system it doesn't exist:


    ```
    ERROR: fenix5plus: C:\Users\myuser\AppData\Local\Temp\BluetoothMeshBarrel-1.0.0.barrel2768611300995056635\content\source\PDUs\TransportAccessPDU.mc:189: Redefinition of variable 'packet'. Previous definition at C:\Users\myuser\AppData\Local\Temp\BluetoothMeshBarrel-1.0.0.barrel2768611300995056635\content\source\PDUs\TransportAccessPDU.mc:142.
    ```

  • A .barrel file is a zip file, and contains the source for the barrel