Acknowledged
over 1 year ago

bug: self.foo behaves different from foo in regards of null check with strict type checker

The following code doesn't compile with SDK 7.2.1 with type check = strict:

import Toybox.Application;
import Toybox.Lang;
import Toybox.WatchUi;

class TestApp extends Application.AppBase {
    public var foo as Number?;

    function initialize() {
        AppBase.initialize();
    }

    public function bar() as Void {
        if (self.foo != null) {
            // this doesn't compile
            self.foo.compareTo(3);
        }

        if (foo != null) {
            // this does compile
            foo.compareTo(3);
        }
    }
}

 

  • I talked with the compiler team, and we need to do some improvements to how we handle null checks. Either case in your example should work.

  • Can't say much about "self", but I've noticed, that null check for class members works only for the first usage of member both using 6.4.2 and 7.2.0:

    if (foo != null) {
        System.println(foo.toString()); // OK
        System.println(foo.toString()); // NOK
    }

    It is even worse:

    if (foo != null) {
        System.println("bar");
        System.println(foo.toString()); // NOK
    }