Problem with registering a Complication Change Callback

Hello!

I'm getting a compilation error on the following line, while trying to register callback for the case when complication has changed:

code: Complications.registerComplicationChangeCallback(method(:complicationChanged));

error: fenix7x: Invalid '$.Toybox.Lang.Method(complicationId as Any) as Any' passed as parameter 1 of type 'PolyType<Null or $.Toybox.Complications.ComplicationChangedCallback>'.

It is inside WatchFace.initialize(), after a given complication is found and subscribed. If the callback registration is omitted, the code is normally compiled and works (except the updated values for given complication, of course).

Can someone please enlighten me on what I'm doing wrong?

  • looks like complicationChanged isn't declared correctly according to the type checker. What's it's declaration?

  • Based on the error message, the callback method is declared without types for the arguments and return value (“as Any”). (Note that you can’t literally use “Any” as a type, so the presence of “Any” in the error message definitively proves that the corresponding types were omitted.)

    While it’s normally ok to omit types with the default level of type checking (which usually only checks types where types have been declared), it doesn’t work when you pass a function as a method into another function (which is why this type of question comes up over and over again). Maybe this could be considered a bug or inconsistency in the type checker, since the dev is forced to declare types in this one case, and again, because this question comes up over and over again.

    Assuming the existing function looks like this:

    function complicationChanged(complicationId) {
      // …
    }

    It needs to change to look like this:

    function complicationChanged(complicationId as Complications.Id) as Void {
      // …
    }

    (See the following API doc page for the ComplicationChangedCallback type definition.)

    [https://developer.garmin.com/connect-iq/api-docs/Toybox/Complications.html]

  • Thank you so much for the explanation! This was incredibly helpful and exactly what I needed to know.