Is there a way to make Flash force static typing?
Is there a way to make the flash as3 compiler require static typing? I've got a bad habit of not doing it, and it'd b开发者_StackOverflow中文版e nice to make it throw compiler errors. In the publish settings for as3, I turned on "strict" but that didn't really change anything.
I am a bit confused. Does this not give you any compile time errors:
function enforceType(var_a:int, var_b:String) {
trace("passed");
}
enforceType(1, 'test');
enforceType(1, 1);
enforceType('test', 1);
It certainly does for me.
EDIT
Since you edited your question I will edit my answer.
AS3 IS statically typed IF you explicitly type your variables.
Example:
var a:int = 0;
a = "TEST";
// a is typed as an int, therefor when you attempt to compile the above,
// you will get a compile time error
// 1067: Implicit coercion of a value of type String to an unrelated type int.
var a = 0;
a = "TEST";
// Here, a is not typed explicitly, so you can assign whatever type you want, and
// the compiler will not complain.
So, in short, AS3 is statically typed if you want it to be. There is no way to make the compiler know what type you actually wanted assigned as it's static type during compilation.
Imagine, for instance, you define a non-typed static variable bar in class Foo.
package
class Foo {
public static var bar;
}
}
Now, in two different places in your program you access Foo. These two different access points are based on events fired due to user interaction like a mouse click. In your two event handlers you have the following:
// In one handler you have
Foo.bar = 1;
// and in another handler
Foo.bar = "test";
The compiler would have no way of knowing which will be run first, since they are both based on user interaction. All the compiler can do is say: If this happens, is it okay? And in this case, since bar is not typed it is okay.
If the Foo class is changed to:
package
class Foo {
public static var bar:String;
}
}
Then the compiler will know that the first event handler listed above is not okay, because bar has been statically typed to be a String.
Long story short (and lesson to be learned): get out of your bad habit and start typing your variables. You'll be glad you did.
For reference.
I use FlexBuilder to write my actionscript. On strong type rule breaking, you'll see warnings. Along with warning for using the same variable name within a scope, unused imports and so forth. This is of course coupled with standard AS3 errors.
I could not continue any longer without a strong IDE.
Alternatives to FlexBuilder of which help in the same manner are FDT and FlashDevlop. Although they might not have all these features
精彩评论