ivanreese
ivanreese

Reputation: 2768

Why is casting Object(null) not null?

When you cast null to an object using Object(null), the result is an object.

When you cast undefined to an object using Object(undefined), the result is an object.

But saying null as Object or undefined as Object remains null.

Why is this? I can't find anything in the documentation about this.

The objects that result from these casts can have properties set on and read from them. It's as if the cast created information, or removed whatever special information the null and undefined values have. But there's no indication in the AS3 documentation that the null and undefined special types are actually objects.

Upvotes: 3

Views: 764

Answers (2)

Manish
Manish

Reputation: 3522

It's easier to explain with a String first.

Let's say we do this:

var v:* = null;
var s:String = v;

What happens? s becomes null. null is a valid value for references of type String.

Now instead:

var v:* = null;
var s:String = String(v);

What happens now? s becomes "null". We're explicitly asking for an object of type String here, but null is of type Null, so it must be converted. null converted to String is "null".

If you were to convert 123.45 to String, you would get "123.45". Fair enough.

Now let's try with Object.

var v:* = null;
var obj:Object = v;

obj becomes null.

Now:

var v:* = null;
var obj:Object = Object(v);

Here again we're explicitly asking for obj to point to a value of type Object - which null is not. null converted to Object is an empty object.

Let's see again:

var s:String = null;

Here the reference s of type String is pointing to the value null of type Null.

Reference String, value Null.

A String reference can take a Null value, but a Number reference cannot take a Null value, so when assigning null to a Number it must be converted (i.e. to 0).

var n:Number = Number(null); // 0

Thinking in terms of references and values really helps.

It also helps to remember that casting is for references, conversion is for values. Object(value) is a conversion. (value as Object) is a cast.

Finally, let's try with a user-defined type.

class Person {}

Then:

var person:Person = null;

Here person becomes null, as you would expect.

But:

var person:Person = Person(null);

person becomes null again! We asked for a Person, but we got a Null. How come?

The value null cannot be converted to type Person. In such a case, the result is the default value for the desired type. The default value for Person is null.

You can see this converting to Number as well.

var n:Number = Number("123.45");

n becomes 123.45.

var n:Number = Number("The quick brown fox, period.");

n becomes NaN.

As the string "The quick brown fox, period." cannot be converted to type Number, the result is the default value for Number, which is NaN. This is in contrast to the previous example where we successfully converted null to Number, yielding 0.

I could go on.

When in doubt, use the as operator. value as Type basically amounts to value is Type ? value : null. No complicated rules to remember. Use Type(value) only when you want to convert a value of one type to a value of another type.

Upvotes: 3

Pixel Elephant
Pixel Elephant

Reputation: 21403

There is a difference between the two casting techniques. Here is a good read on it: http://upshots.org/actionscript-3/as3-casting-objects

Basically, Object(null) actually converts null into an Object while "as" attempts to see if null can be treated as an Object, which it cannot.

Upvotes: 3

Related Questions