Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does casting a Date to a Date in ActionScript fail?

In ActionScript, I've discovered, casting a Date to a Date and assigning it to a Date-typed variable throws a TypeError:

var date : Date = Date(new Date(2012, 01, 01));

Error #1034: Type Coercion failed: cannot convert "Wed Aug 22 17:06:54 GMT+1000 2012" to Date.

This is obviously wrong, but I'd like to know why it happens. My theory is that the Date cast, like the Number cast, has been overridden to attempt to convert the given type rather than just cast it.

Interestingly, casting anything else to a Date and assigning it to a Date also fails:

var date : Date = Date("1/2/3");
var date : Date = Date(123);

// (Both fail)

But assigning it to an Object succeeds:

var object : Object = Date(new Date(2012, 01, 01));
var object : Object = Date("1/2/3");
var object : Object = Date(123);

// (All succeed)
like image 758
Will Madden Avatar asked Dec 01 '25 09:12

Will Madden


1 Answers

AS3 can be very confusing and inconsistent at times. Basically you're not casting anything in that code sample.

AS3 has some global camelCased functions that will take precedence over casting operators. Vector also has a similar global function.

When you do Date(bla) without the new operator, it apparently creates a string representation of that date... Try to cast with the as operator instead.

like image 67
AlexG Avatar answered Dec 04 '25 06:12

AlexG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!