Reputation: 6945
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)
Upvotes: 3
Views: 227
Reputation: 13327
Typically you should get a compiler warning about this behaviour, if the compiler argument
<!-- Invalid Date cast operation. -->
<warn-bad-date-cast>true</warn-bad-date-cast>
exists in your flex-config.xml.
Upvotes: 1
Reputation: 4045
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.
Upvotes: 5