4 Replies Latest reply on Nov 23, 2010 7:30 AM by rtalton

    for ... in loop giving me an "implicit coercion" error--but describeType says I've got right type?

    JoshBeall Level 1

      Hi All,

       

      I've got the following code:

       

      var currentStateIndex:int = 0;
      for (var index:int in this.states){
           if(this.currentState == this.states[index].name){
                currentStateIndex = index;
                break;
           }
      }
      

       

      I'm trying to identify the index of the current state.  It's giving me an error on the for line, saying "Implicit coercion of a value of type String to an unrelated type int."  Hmm, strange, I thought--wouldn't the keys of the this.states array (and it is just a plain vanilla array) be integers?

       

      So, I tried this:

       

      var currentStateIndex:int = 0;
      for (var index:* in this.states){
           var theType:* = flash.utils.describeType(index);
           if(this.currentState == this.states[index].name){
                currentStateIndex = index;
                break;
           }
      }
      

       

       

      I used the debugger to inspect the value of theType.  It's this:

       

      <type name="int" base="Object" isDynamic="false" isFinal="true" isStatic="false">
        <extendsClass type="Object"/>
        <constructor>
          <parameter index="1" type="*" optional="true"/>
        </constructor>
      </type>
      

       

      So, indeed, describeType says that the index value is of type "int"... so why can't I strongly type it as an int?