Acknowledged

Some typchecking 1 (gradual) issues

First a bug

1) Some namespacing/alias issue with has

 

using Toybox.WatchUi as Ui;

// ... code here
// eventually
if (Ui has :TextPicker) {
 // do things
}

// Yields error:
ERROR: fenix6xpro: file:lineno: Undefined symbol ':Ui' detected.
// Needs to change to WatchUi and/or Toybox.WatchUi to not error.

Secondly a ... You may file this as a bug or a feature, because I'm torn between what it is. I just know it is a tad-bit annoying.

// Let's assume the following code:
  function refresh() {
    var ids = $.profiles.getIds();
    for (var i =0; i< ids.size(); i++) {
      var shim = $.profiles.getShim(ids[i]);
      upsertProfileShim(shim);
    }
    if ($.profileDeletedId != null) {
      M2.deleteItem(self, $.profileDeletedId);
    }
  }
  
// and the following code for the profile getIds() method
  function getIds() as Array<String> {
    var array = [] as Array<String>;
    array = self.profiles.keys();
    return array;
  }
  
  
// equally frustrating, of not more

class Bar {
  var thing as Array<Numeric>
  
  function initialize() {
  
     thing = [ 1, 2 ]; // yields and warnings
     // must be changed to
     thing = [ 1, 2 ] as Array<Numeric>;
     // I just told you what it is
  }
}

It's insanely annoying that even when we sprinkle the code in getIds() with typing information that the callers need to do the very same thing on a container. We don't have to do this for Strings or other type of objects.

  class Foo {
    function initialize() {
    }

    function foo() as Foo {
      return new Foo();
    }
  }
  
  // This works fine, we don't need to tell anyone we are getting an object Foo.
  var f = new Foo();
  var b = f.foo();
  var c = b.foo();

3) I came across one more thing:

Toybox.Application.Storage as st;
// .. 

st.setValue("profiles", {}); // error
// Work around it
var d = { 0 => 1 };
d.remove(0);
st.setValue("profiles", d);

// Or set this piece of weirdly looking code
st.setValue("profiles", {} as Lang.Dictionary<String, Null>);

But even when you want to enforce this, please, move the warning to gradual (level 2). If you want to pedantic, be it in informative.
I want type checking 1 for checking input params at functions, but the container warnings are a bit too much.

  • The code works, but the typechecker is not happy about it. And because it is an error it doesn't want to compile it. 

    Yeah, I mean that it compiles and runs with the 4.1.4 Compiler 2 Beta - I tested with -L 1 and -L 2 type checking.

    Hopefully I didn't make a mistake there.

  • The code works, but the typechecker is not happy about it. And because it is an error it doesn't want to compile it. 

  • Oh, and just for completeness (and to verify that it's just a coincidence that WatchUi actually *does* have the symbol foo lol, as opposed to a bug with "has")

    import Toybox.WatchUi;
    using Toybox.WatchUi as Ui;
    ...
        function test() {
            if (Ui has :foo) {
                System.println("whoa");
            }
            if (WatchUi has :foo) {
                System.println("wowow");
            }
            if (Ui has :foo2) {
                System.println("quite");
            }
            if (WatchUi has :foo2) {
                System.println("unexpected");
            }


    Output:
    whoa
    wowow
  • regarding the "using" it is documented (but still annoying I agree). I never use using...

    Where is it documented if I may ask?

    I could be wrong, but maybe Gavriel is referring to the fact that if you use "import" (instead of "using"), then you're able to refer to a type without its module prefix?

    The module alias thing you reported seems like a bug to me. It works fine without type-checking (obviously).

    I checked with the beta compiler, and the bug seems to be fixed already. (Tested with "-L 1" and "-L 2")

    using Toybox.WatchUi as Ui;

    ...
        function test() {
            if (Ui has :foo) {
                System.println("whoa");
            }

    ...
  • Where is it documented if I may ask?