Acknowledged
CIQQA-3043

View.findDrawableById() doc code example does not compile and is arguably misleading/confusing

Original title: "View.findDrawableById() incorrectly used in doc code example as a static method"

Like many of the doc code examples, it simply does not work, but in this case, it's also highly misleading, as it suggests that findDrawableId is a static method, rather than an instance method.

https://developer.garmin.com/connect-iq/api-docs/Toybox/WatchUi/View.html#findDrawableById-instance_function

EDIT: as flocsy pointed out, if you imagine that the sample code is wrapped in a View-derived class, all of a sudden the code "View.findDrawableById()" takes on a completely different meaning. The code sample as-is is still not great, especially for noob coders who would not automatically know all of of this stuff.

(If they did, they probably wouldn't need the sample.)

  • Imo, in this case, anyone who is able to understand and fix the sample code in question clearly does not need the sample code in the first place

  • Yeah, good point.

    In the context of a View-derived class, View.findDrawableById() would have a different meaning (call the implementation of findDrawableId() on the View class, with self set to the current View-derived object).

    It still doesn't make much sense, as there's little reason to call View.findDrawableById() instead of findDrawableById(), unless it's within an overridden implementation of findDrawableById(). (Maybe one reason could be to optimize symbol lookups, same with how "$" is used to explicitly look things up in the global scope.)

    But the meaning of the code would def change to something less misleading / incorrect.

    More bugs in the sample:

    - "using Toybox.WatchUi.View;" should be "using Toybox.WatchUi;"

    - "view.setText(timeString);": this will fail if type checking is enabled. Couple of ways to fix this

  • oooor. it could make some (though arguably not much) sense if it was wrapped in:

    class MyView extends WatchUi.View {

    }

    Then it would probably compile and work.

  • A proper example would:

    - show that findDrawbleById() should be called in the context of an existing view which contains the drawable in question

    - would especially not have code that incorrectly accesses View.findDrawableById() as it were a static method

    - would use a variable name such as "drawable" to store the result, not "view"

  • Not only will this code snippet not compile, it also doesn't make any sense.