cannot find symbol :setText after updating SDK

Cannot find symbol ':setText' on type 'PolyType<Null or $.Toybox.WatchUi.Drawable>'

I get a bunch of those errors after I updated to latest SDK.  I am not sure what version I had before but it was probably pretty old.  I am still using eclipse btw...

Code is like:

var hourView = View.findDrawableById("HourLabel");

hourView.setText(hourString);

I don't want to update but it seems that maybe I must in order to support newer watches.  (??)

How do I fix this?  Thanks for any suggestions.

  • Starting with 4.1.6, type checking is on by default.  You can turn it off on a per-project basis by adding this to your monkey.jungle file:

    project.typecheck = 0

  • That did the trick, thanks.  Am I supposed to be casting everything?  I don't normally use strict typed languages.

  • I turn it off on my long term well established apps.  For new apps I can use the default for the extension in VS Code..

  • Am I supposed to be casting everything?  I don't normally use strict typed languages.

    My 2 cents is that in a robust implementation of strict typing, you should rarely (if ever) have to cast anything. It should be enough to use type declarations, type narrowing, etc. Anyone who regularly codes in TypeScript will have seen bugs related to explicit type casts, because type casts sidestep type checking and give you a false sense of security - "I'm using strict type checking, so it's impossible for my app to have type errors"

    Unfortunately, in the current implementation of Monkey Types, there's a few situations where casting is unavoidable. And the implementation of type casting is very relaxed in Monkey C. IIRC, in Monkey C, you can cast any type to any other type, which furthers the point I made in the previous paragraph. At least in a language like TypeScript, you'll get an error if you try to directly cast between incompatible types -- TypeScript requires you to first cast to "any", then cast to the incompatible type. (This is a good thing because it becomes immediately obvious to someone looking at the code when a typecast is extra dangerous).

    It doesn't help that the syntax for type declarations (good/safe) and type casts (bad/dangerous) is very similar (both use the keyword "as"). The only difference is the context in which "as" is used.

  • You can fix this while type checking is enabled by casting the result of  `findDrawableById`, so in your case

    var hourView = (View.findDrawableById("HourLabel") as Toybox.WatchUi.Text);

    should do the trick.

  • or:

    var hourView = View.findDrawableById("HourLabel");

    if (hourView != null) { 
      hourView.setText(hourString);
      // optionally if this doesn't work (there was a bug reported about this, I don't remember if it was fixed already), then:
      (hourView as Drawable).setText(hourString);
    }