flex-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Harbs <harbs.li...@gmail.com>
Subject Re: FlexJS XML string assignment
Date Fri, 05 Aug 2016 08:59:24 GMT
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(name+ “ Flintstone”); //Fred Flintstone

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

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

That would let XML act as if it’s a string (and number) in most cases.


On Aug 4, 2016, at 5:33 PM, Alex Harui <aharui@adobe.com> wrote:

> On 8/4/16, 7:22 AM, "Harbs" <harbs.lists@gmail.com> 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

View raw message