AlecMcE.com

Coding on the Flash Platform

Archive for the ‘const’ tag

XML Constants Gotcha

View Comments

In my head, XML slips between the cracks of being an Object and a primitive type. It shouldn’t, and when I’m concentrated it doesn’t. But still, there is something primitive-type about it.

private const DATA:XML = <data message="Hello World!" />;

and

private const DATA:String = "Hello World!";

are intuitively the same sorts of things. XML is like an advanced sort of String. We encode it as we encode text, and we convert it to and from Strings without difficulty. I don’t construct XML, at least it doesn’t feel like I do. In my preferred coding environment (FDT) by default they’re even similarly coloured.

However, they are clearly not the same, because XML is mutable:

DATA.@message = "This message has been changed!";

Needless to say, that doesn’t work on a String! Somehow by labelling the XML a ‘constant’ I expect the XML to be immutable.

On reflection I know this about XML, but it feels wrong. This ‘gotcha’ does not identify a language bug, but suggests a mental corrective: when you think about XML, make sure that you don’t mistake it for a primitive type. When I’m not concentrating I do, and it occassionally trips me up.

Written by alec

August 13th, 2009 at 1:11 pm

Posted in as3,gotcha

Tagged with , ,