I’m thinking that I should implement valueOf() for XML like this:
var str:String = this.toString();
var asInt:int = parseInt(str);
if(asInt.toString() == str)
return asInt;
var asFloat:Number = parseFloat(str);
if(asFloat.toString() == str)
return asFloat;
return str;
This would make XML work natively with primitives in most cases even if we
can’t infer types:
var xml:XML = <xml name”Fred” value=“10”/>;
var name:* = xml.@name;
var value:* = xml.@value;
trace(value+5)//15
trace(name+ “ Flintstone”); //Fred Flintstone
trace(“10”+value)//“1010”
trace(value+value)//20
This will not work if we’re expecting a valid number to actually be a string.
We could end up with arithmetic instead of string concatenation.
It will also not allow things like:
name.toLowerCase()// error
name.split(“”);//error
One hack we could do would be to add all string and number methods to XML and
XMLLIst which would essentially call toString(). Something like this:
public function
split(separator:*=undefined,limit:int=Number.POSITIVE_INFINITY):Array
{
this.toString.split(separator,limit);
}
That would let XML act as if it’s a string (and number) in most cases.
Thoughts?
On Aug 4, 2016, at 5:33 PM, Alex Harui <[email protected]> wrote:
>
>
> On 8/4/16, 7:22 AM, "Harbs" <[email protected]> wrote:
>
>> I’m not sure how to deal with this case:
>>
>>
>> private var name:String;
>>
>> this.name = someXML.@Name;
>>
>> The above compiles to
>>
>> this.name = someXML.attribute('Name’);
>>
>> In Javascript this.name becomes an XMLList, but in Flash, the XMLList is
>> implicitly converted to a string.
>>
>> Is it possible to make the compiler smarter and make the compiled version
>> look like this?
>>
>> this.name = someXML.attribute('Name’).toString();
>
> I will look into it.
>
> -Alex
>