Updating code from Connect IQ version 4.1.5 to 6.2.1

Code such as:

var data as ByteArray;

and

 

function compute(info as Activity.Info) as Void {

bleHandler.compute();

}

compiles using version 4.1.5.

However on version 6.2.1, compiles errors stating the below:

Invalid explicit typing of a local variable. Local variable types are inferred.

How can this code be compiled on version 6.2.1 so that simulators for new watches such as Forerunner 965 can be used to test the data field?

  • var data as ByteArray

    Trying to explicitly type local variables has been an error ever since the type checker was introduced, but prior to 4.1.6 it was accepted (and the "as ByteArray" was ignored) as long as you didn't turn the type checker on. In 4.1.6 and later its an error whether or not the type checker is enabled.

    The best way to make it compile is to remove the invalid type declarations.

    function compute(info as Activity.Info) as Void

    You don't say what went wrong with this version, but my first guess would be that you didn't import Toybox.Activity? Again, prior to 4.1.6 that was ok as long as you didn't enable the type checker.

    My second guess is that something is failing to type check; in which case, you can just turn the type checker off in your project's settings (again, in older compilers, the type checker was off by default, now its on by default). But a better fix would be to fix the errors the type checker is reporting.

  • var data as ByteArray

    What is the correct way to fix this invalid type declaration?

    Thanks for your response, you were correct about import Toybox.Activity

  • Try just doing

    var data=[]b;

    that gives you an empty ByteArray.  The "b" indicates a ByteArray.

    For example, with this

    		var data=[]b;
    		System.println("BA? "+(data instanceof ByteArray));
    		System.println("sz="+data.size());

    The output is

    BA? true
    sz=0
    What you are doing pre-typechecking is the same as just doing
    var data;
  • Thank you, this is new to me.

    So 'var data as ByteArray;' could be written as 'var data;'

    Also 

    'var text as String =" "' could be written as 'var text;' and then object can be defined later in code?

  • With type checking off,

    var data;

    is a variable that's null (a boolean).  It's not a ByteArray

    var text="";

    is an empty String,

  • It depends on the context. If it's a local variable (a variable which is defined at the function scope), then you must not provide a type. Providing a type for local variables is what causes the following error message: "Invalid explicit typing of a local variable. Local variable types are inferred." The error message is saying: don't provide a type for local variables, the compiler will figure it out.

    If it's a variable defined at the class or global scope, then it is still useful to provide a type (and depending on your type-checking level, it may be mandatory).

    // example.mc
    import Toybox.Lang;
    
    var globalVariable as String = "foo"; // type can still be specified here
    
    class AwesomeClass {
      var classVariable as Number = 42; // type can still be specified here
      
      function interestingMethod() as Void {
        var localVariable = 11; // type cannot be specified here
      }
    }

  • var data;

    is a variable that's null (a boolean).  It's not a ByteArray

    ?

  • Thanks for the stealth edit where you deleted a condescending response which implies that I didn't know what I was talking about, when in fact you were the one in error. Because you never consider the possibility that when someone else contradicts something you said, it might be you that made the mistake instead of the other person.

    Also, thanks for clarifying that null is in fact "a null".

    Try it

    Par for the course, really. Your answer is still not quite right. Since we are obviously talking about compile-time types (not run-time types), "var data;" (at the class or global scope) is untyped. The initial value may be null, but that's not the same as the compile-time type (for the purposes of compile-time type checking.)

    class ExampleClass {
        var data;
    }

    WARNING: fr945: Example.mc:34: Member '$.ExampleClass.data' is untyped.

    You also sort of missed the point of the comment that you were replying to and perhaps the OP.  They're not asking if "var data as ByteArray;" and "var data;" are exactly the same, they're asking if the former can (and should) be replaced with the latter. (Recall that this stems from the error message that appears when a local variable is explicitly typed.) The actual answer is that it depends on whether data is a local variable or not:

    - If data is a local variable, then you actually *must* use "var data;" and not "var data as [TYPE];" (since, as the error message in the OP states, explicit types for local variables are forbidden.)

    - If data is not a local variable, then it's better to explicitly specify its type than to leave it untyped. As matter of fact, if you use strict typing (-l 3), then it's mandatory

  • Thank you, this explains it perfectly! They are both local variables.