Using annotations

The documentation briefly mentions annotations, but it doesn't mention how to use them or what they could be used to do. Could someone describe how to use them and why they might be useful?

(:test) function print(s) {
Sys.println(s);
}


Travis
  • Currently they're not used when compiling an app, but are used throughout the API. Your example provides a good use of them, and how to correctly use them, once we have the testing framework in place. They can be used to identify functions that are to be run as test cases. For instance, any function that has the annotation of (:test) must return a Boolean indicating pass or failure. Then the testing framework can identify all test functions, call them, and let you know which ones fail.
  • Can you give an example of how they are used throughout the API and how I'd interact with classes/modules/methods that are annoted?
  • One example is the Attention module. The playTone() function is only available to certain devices, so it's annotated as such in the API. Then in your code you do

    using Toybox.Attention as Attention;
    function onTap(evt) {
    if( Attention has :playTone ) {
    Attention.playTone();
    }
    }


    The annotation in this case cause the playTone() function to be available on those devices that have a tone generator.
  • So my question is, how can I label a function in my class/module API so that it is available only conditionally? This is being done with Attention.playTone(), so it seems that I should be able to do this in my code, but I've seen nothing shows how to do it.

    Travis
  • As far as I know annotations aren't used when compiling apps at this time. I may be wrong, and if so Alpha will come in and correct me, but I think the annotations aren't currently looked at by the compiler when compiling an app.
  • No, annotations aren't yet used during compile time. It's something we've discussed, though, since you could do something like annotate functions as test functions which would run in the sim for unit testing, but would not be included when compiled for a device.
  • Former Member
    Former Member over 9 years ago
    labelling way points

    Hi all,

    Has anyone got information how to make the annotation/label come up on a waypoint?

    I have a Garmin Monterra which the annotation comes up when i load the gpx files from Arcmap Via basecamp.

    However since updating to software version 4.10_1.28.00 the labell disappeared.

    Thanks in Advance
  • Has anything changed with regards to annotations? So there are build file exclusions that help here. But if I want code for just one platform, I basically have to add build file exclusions that go both ways.


    (:excludeEpix) function A() { /*empty do nothing function */}
    (:isEpix) function A(){/*work around for epix */}

    So I would have to add
    <exclude annotation="isEpix"> for everything except the epix build where I'd add
    <exclude annotation="excludeEpix">

    Is there anyway to do a ! annotation so that I only would have to define the annotation in the epix resources. Something like:

    (: ! isEpix)

    Further, I believe these annotations can only be used on functions and classes so I have to define functions and variables for all cases, just with different behavior in them. Even though in many cases the function would be empty.
  • Thanks. Lots of good info there. The multiple exclusions is interesting. This all helps.

    One thing I just noticed trying this out on const's and var's is that you will get errors when you try to save the .mc file if you haven't already compile with the annotation exclusion in the resource.xml file. Meaning, I added the annotation exclusion to the resource file and saved it. Then modified the .mc file to take advantage of it and it kept telling me I had duplicate definitions and gave me errors when I'd save the .mc file. Once I did the build, however, it resolved the problem.

    I have a feeling I ran into this before which lead me to believe it didn't work on const's and var's. Having it work on these will actually be very helpful for keeping the code smaller.

    I'm still mentally having a hard time wrapping me head around the fact that they are exclusion annotations rather than inclusion annotations. Coming up with proper names, such as "exclude..." might help here.
  • I'm still mentally having a hard time wrapping me head around the fact that they are exclusion annotations rather than inclusion annotations.

    Yes, I agree. I typically use is_<whatever> and not_<whatever> as the annotation name. For example is_epix and not_epix. Then in resources-epix/exclusions.xml I exclude not_epix and everywhere else I exclude is_epix. At least that makes the most sense to me.

    I really wish that they had a <define annotation="name" value="true" /> resource, and then supported boolean logic. By default, all unannotated declarations would be included. If there is an annotation on a declaration, the boolean expression is evaluated; if it evaluates to true the code is included. If an annotation is not defined, it is assumed to be false. This would be much more powerful.

    Travis