‘as’ isn’t the equivalent of typing.

I guess I hadn’t thought too deeply about it, but I always thought that the ‘as’ Operator was the equivalent of typing a variable.

pseudo code example:

type(test) = test as type

or to give a real world example:

var myVar:String=’3′;
var myInt:int=int(myVar);

or alternatively:

var myInt2:int=myVar as int;
trace(myInt,myInt2);

But this code will trace:
3 0

I’ve realised that while typing a variable actually converts the variable to the requested datatype, ‘as’ merely informs the compiler to expect a certain variable to be of a certain datatype.

In the above example, ‘myString as int’ doesn’t work because myString isn’t an int.

However, if initially we had defined myVar like so:

var myVar:*=3;

The code would have no problem as myVar is an int.

Interactive developer, Author - iOS development with Swift - book coming 2017 https://manning.com/books/ios-development-with-swift

Tagged with:
Posted in Flash

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: