Unable to detect scope 'Time.Moment' for the symbol 'initialize'.

I am tidying up my code, to remove the many warnings that I'm getting, but am having trouble interpreting the problems.

Here's one:

WARNING: fenix5: {redacted file path}.mc:241: Unable to detect scope 'Time.Moment' for the symbol 'initialize'.

on code:

var timestruct = Gregorian.info((new Time.Moment(timeStamp )), Time.FORMAT_SHORT);

What does it mean and how do I clear the warning?

  • using Toybox.Time.Moment;

    at the top of your code.

  • Nope, that generates an ERROR:

    using Toybox.WatchUi as Ui;
    using Toybox.System as System;
    using Toybox.Application.Storage as Storage;
    using Toybox.Time.Gregorian as Gregorian;
    using Toybox.Time.Moment;

    ERROR: fenix5: {file path redacted}.mc:5: Unknown module Toybox.Time.Moment in using or import clause.

  • Change:

    using Toybox.Time.Moment;

    to

    using Toybox.Time;

    The warning in your original post happened because you should use/import the Toybox.Time module if you want to access one of its members, like the Time.Moment class. I don't know why this isn't an error, to be honest.

    The error in your followup comment happened because Toybox.Time.Moment is not a module, it's a class.

  • OK, so I replaced  using Toybox.Time.Moment; with using Toybox.Time; and the error and warning disappeared.

    That's the answer to my second question, thanks.

    Now, to the first question: what does it mean?

    I'm not a computer science grad, just an old software engineer used to Unix, C, PHP etc, with a reasonable exposure to C++ and Java, but this is all very strange to me.

    My experience tells me that something flagged as a warning now will become an error in later versions, so I really would like to get my head around it.

  • Now, to the first question: what does it mean?

    It means that when you call new Time.Moment(), the compiler implicitly wants to call a function called initialize() in the scope Time.Moment, but because you lack the a using or import clause for Toybox.Time, the compiler has insufficient (?) information about that scope.

    What the practical consequences of that warning are, I couldn't say. I tried similar code with and without the import, and both versions would check the constructor for the correct number of arguments, for example (assuming type checking is enabled).

    In other words, type-checking works with the import, and the code runs without the import, so why is the import necessary? And why is there a warning if it's missing?

    In most languages which use imports (e.g. TypeScript, Java), a missing import is a hard error, not a warning.

    I can't really answer those questions, but somebody who works at Garmin could.

  • Nice response, thanks, glad to hear I'm not the only one confused by this. 

    In other words, type-checking works with the import, and the code runs without the import, so why is the import necessary? And why is there a warning if it's missing?

    All very good questions. Let's hope our little discussion is picked up by the Garmin team.