For cases where the compiler knows that XML is being assigned to a string, it should add toString().
If it knows it’s being assigned to a Number, I think it should call valueOf(), or possibly(Number(xml.valueOf()) There will likely be cases where the compiler will not know the types that the XML is being assigned to. It would be good if the right thing should automatically happen 95% of the time even in those cases. I’m not suggesting that the compiler adds valueOf() with the exception of assignment to Number variables. Javacript engines call toString() and valueOf() automatically when it’s expecting a primitive type (such as comparisons to primitives or for operators which work on primitives). That’s why I care about fixing valueOf(). As far as ints go, the reason I’m suggestion making a separate parseInt() call is for cases when you have something like this: 0xff00ff (although I realized that my logic is flawed and needs to be fixed.) On Aug 5, 2016, at 5:30 PM, Alex Harui <aha...@adobe.com> wrote: > I have not spent time thinking about this, but the compiler generally > knows the destination type. The compiler is going to have to learn when > to inject coercion code where AS would do an implicit conversion that JS > won't. > > So fundamentally: what does XML valueOf do in AS? The XML JS > implementation should probably do exactly the same thing. I'm not sure it > needs to distinguish between ints and floats since there is no int in JS. > > The compiler will be injecting toStrings and Number() calls. It isn't > clear the compiler can/should inject valueOf calls. > > Of course, I could be wrong. > -Alex > > On 8/5/16, 5:28 AM, "Harbs" <harbs.li...@gmail.com> wrote: > >> I implemented this locally and the idea works pretty well for the most >> part. >> >> We still need to do toString() when possible because of edge cases. >> >> For example: stringFromXmlList1 == stringFromXmlList2 fails because the >> Javascript engine does not try to convert them (correctly) to primitive >> values. >> >> On Aug 5, 2016, at 11:59 AM, Harbs <harbs.li...@gmail.com> wrote: >> >>> 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 <aha...@adobe.com> wrote: >>> >>>> >>>> >>>> On 8/4/16, 7:22 AM, "Harbs" <harbs.li...@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 >>>> >>> >> >