On Thu, Jan 7, 2016 at 4:55 PM, Alex Harui <aha...@adobe.com> wrote:

> How many of you use the "as" keyword as part of a test?


I have a huge code base and I use "as" / "is" everywhere (
https://github.com/WeaveTeam/Weave). My code depends on it behaving the
same way it does in ActionScript.  IMO, being able to easily check if an
object extends or implements a class/interface is a huge advantage AS has
over JS.


> The reason I'm asking is because the use of "as" as become a negative
> factor in several cases:
> 1) In JS, it results in a function call
>

Yes. In ActionScript, using "as" actually sped up the code in certain
situations, but in JS it is a performance hit.  This is a very common
pattern in my code:

if (obj is Thing1)
    (obj as Thing1).foo(a,b);
else if (obj is Thing2)
    (obj as Thing2).bar(x,y);

Each pair of is/as results in redundant processing since Language.as()
calls Language.is() internally, so I've been changing my code like this to
improve performance:

var thing1:Thing1 = obj as Thing1;
var thing2:Thing2 = obj as Thing2;
if (thing1)
    thing1.foo(a,b);
else if (thing2)
    thing2.bar(x,y);


> 2) As Om noted yesterday, it doesn't work for Native JS types
>

It does work with native types:

org.apache.flex.utils.Language.is(document.createElement('button'),
HTMLButtonElement)
-> true


> 3) It causes unnecessary class dependencies which complicates the
> goog.requires list
>

The class dependency is necessary if you need the "as" keyword to behave
the same way it does in ActionScript (like I do).


>
> Currently there is an @flexjsignorecoercion hack you can use to tell
> FalconJX to skip code-generation for an "as" operation.  However, we are
> adding more and more of these, and they are more frequently the cause of
> something not working, so I am thinking about flipping the logic and not
> generating code for any "as" operation unless you specifically ask for it
> via @flexjsgeneratecoercion or something like that.
>
> Thoughts?
> -Alex
>
>
I see "is" and "as" as highly useful features, and I think it would be a
mistake to make the code cross-compile into something that behaves
differently by default.  IMO if cross-compiled code behaves differently
than the original then it's being mangled and can't be trusted.

A related issue is when setting a typed variable or passing in a parameter
to a function, it will do type coercion automatically in AS but that
behavior is lost when cross-compiling to JS.  For example, I have
situations like this where I now have to add manual type casting, and I
wish the compiler would do that automatically:

var str:String = value_which_may_be_a_number;

Reply via email to