4 Replies Latest reply on Dec 16, 2011 3:24 PM by drkstr_1

    Bizzare XML const appendChild() behavior

    mitz11

      I'm using the appendchild() function to append some stuff to the end of an XML variable, and it's overrwirting a constant.

       

      The code that looks something like this:

       

      private const CONSTANT_XML:XML = <ExampleThing1><ExampleThing2></ExampleThing2></ExampleThing1>;

       

      private function onCreationComplete():void

      {

         fun1();

         fun2();

      }

       

      private function fun1():void

      {

         var xmlThing1:XML = CONSTANT_XML;

         xmlThing1.appendChild(<blah/>);  //THIS ALTERS the CONSTANT_XML constant!  what the heck?!

      }

       

      private function fun2():void

      {

        var xmlThing2:XML = CONSTANT_XML;

        trace(xmlThing2);  //outputs: <ExampleThing1>  <ExampleThing2/>  <blah/></ExampleThing1>

        trace (CONSTANT_XML); // outputs: <ExampleThing1>  <ExampleThing2/>  <blah/></ExampleThing1>

      }

       

       

       

      Is this a known bug?  Am I doing something wrong?  I thought constants were.... y'know, constant.

        • 1. Re: Bizzare XML const appendChild() behavior
          mitz11 Level 1

          Oh yeah! 

          tested with SDK 4.5.1A

          and SDK 4.6.0

          should have said that.

          • 2. Re: Bizzare XML const appendChild() behavior
            drkstr_1 Level 4

            Well constants are usually meant to be scalar values, so I've never really tried this before, but it would seem the constant only applies to the reference and not the properties contained within the reference. I just tested it with a basic Object, and was able to update properties on it just fine.

            • 3. Re: Bizzare XML const appendChild() behavior
              mitz11 Level 1

              Yeah, it's strange. I see what you're saying about the const applying only to the reference, and not it's contents.  That makes sense.

               

              On a related note, I was hunting through the known bugs, trying to find out if this is a known issue, or if it's considered an issue at all.  I didn't find reference to this problem exactly, but I found something similar that really surprised me:

               

              You cannot do:

               

              const i:int = 0;

              i = 1;  //compile error!

               

              but you CAN do:

               

              const i:int = 0;

              i++;  //i = 1

               

              I wonder if this is at all related?

              • 4. Re: Bizzare XML const appendChild() behavior
                drkstr_1 Level 4

                Hmmm, very curious. I would say the ability to increment a constant is a bug, not a feature. Although, I can see why they didn't bother with locking all properties in a complex object, as this is usually not how constants are used (at least in a language that passes all non-scaler values by reference). In other languages, you would just pass by value to avoid damaging the original.