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)
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.
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.
Related
As a complete Dart beginner coming from python and javascript, I find this behavior quite strange:
var user = {'name': 'John Doe', 'birth_y': 1980};
2021-user['birth_y'] // is 41
But if the operator is on the right.
user['birth_y'] + 41 // error
The error:
Error: The operator '+' isn't defined for the class 'Object'.
- 'Object' is from 'dart:core'.
Try correcting the operator to an existing operator, or defining a '+' operator.
user['birth_y'] + 41;
^
From the error, one guesses that for maps with mixed types, entries have type 'Object', Yet:
user['birth_y'] is int // true
user['birth_y'].runtimeType // int
This behavior is also exhibited by lists of mixed types,
What am I missing?
Dart does a statically analysis of your program before running it. In this analyze phase, it will look at the data type of your variables and see if it is statically safe.
In your case you have this Map:
var user = {'name': 'John Doe', 'birth_y': 1980};
Since your Map contains different types of values, Dart will try to see which data type can be used for all your values. In this case, the only thing String and int shares is the Object type. So user is being analyzed to be of the type Map<String, Object>.
This means that when you get a object from user, the only thing the analyzer can be sure about is it is of the type Object.
So when you do:
user['birth_y'] + 41
The analyzer will assume you get a Object which does not have the + operator.
You should in general not use Map as some kind of object which contains different data types. Instead create a class which makes it possible to write type safe code.
I try this code to get the events that their startdate equal to "Today":
`var ref = new Firebase("https://event-application.firebaseio.com/event");
$scope.evt=$firebaseArray(ref.child('event'));
var a= new Date();
ref.orderByChild("startdate").equalTo(a).on("child_added",function(snapshot){
console.log(snapshot.val() )
})`
But i get this error:
First argument passed to equalTo() cannot be an object
It happens because you are using a Date object.
Check the documentation:
equalTo()equalTo(value, [key])
Arguments
value String, Number, Null, Boolean
The value to match for. The argument type depends on which orderBy*() function was used in this query. Specify a value that matches the orderBy*() type. When used in combination with orderByKey(), the value must be a string.
System::DateTime ^now = System::DateTime::Now;
System::DateTime ^now2 = System::DateTime::Now;
System::TimeSpan ^span = now->Subtract(now2);
The above code gets the compiler error:
test.cpp(104) : error C2664: 'System::TimeSpan System::DateTime::Subtract(System::DateTime)' : cannot convert parameter 1 from 'System::DateTime ^' to 'System::DateTime'
1> No user-defined-conversion operator available, or
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
now and now2 are c++/cli handles, but the method expects the actual object. What is the correct way of invoking the method from c++/cli?
DateTime and TimeSpan are value types.
System::DateTime now = System::DateTime::Now;
System::DateTime now2 = System::DateTime::Now;
System::TimeSpan span = now.Subtract(now2);
System::Diagnostics::Stopwatch^ stopwatch = gcnew System::Diagnostics::Stopwatch();
stopwatch->Start();
// code
stopwatch->Stop();
// stopwatch->Elapsed
This seems quite bizarre to me and totally putting me on the side of people willing to use plain java. While writing a groovy based app I encountered such thing:
int filesDaily1 = (item.filesDaily ==~ /^[0-9]+$/) ?
Integer.parseInt(item.filesDaily) : item.filesDaily.substring(0, item.filesDaily.indexOf('.'))
def filesDaily = (item.filesDaily ==~ /^[0-9]+$/) ?
Integer.parseInt(item.filesDaily) : item.filesDaily.substring(0, item.filesDaily.indexOf('.'))
So, knowing that item.filesDaily is a String with value '1..*' how can it possibly be, that filesDaily1 is equal to 49 and filesDaily is equal to 1?
What's more is that when trying to do something like
int numOfExpectedEntries = filesDaily * item.daysToCheck
an exception is thrown saying that
Cannot cast object '111' with class 'java.lang.String' to class 'int'
pointing to that exact line of code with multiplication. How can that happen?
You're assigning this value to an int:
item.filesDaily.substring(0, item.filesDaily.indexOf('.'))
I'm guessing that Groovy is converting the single-character string "1" into the char '1' and then taking the Unicode value in the normal char-to-int conversion... so you end up with the value 49.
If you want to parse a string as a decimal number, use Integer.parseInt instead of a built-in conversion.
The difference between filesDaily1 and filesDaily here is that you've told Groovy that filesDaily1 is meant to be an int, so it's applying a conversion to int. I suspect that filesDaily is actually the string "1" in your test case.
I suspect you really just want to change the code to something like:
String text = (item.filesDaily ==~ /^[0-9]+$/) ? items.filesDaily :
item.filesDaily.substring(0, item.filesDaily.indexOf('.'))
Integer filesDaily = text.toInteger()
This is a bug in the groovy type conversion code.
int a = '1'
int b = '11'
return different results because different converters are used. In the example a will be 49 while b will be 11. Why?
The single-character-to-int conversion (using String.charAt(0)) has a higher precedence than the integer parser.
The bad news is that this happens for all single character strings. You can even do int a = 'A' which gives you 65.
As long as you have no way of knowing how long the string is, you must use Integer.parseInt() instead of relying on the automatic type conversion.
There's the code:
HebrewCalendar Heb = new HebrewCalendar();
DateTime tmp = new DateTime(1964,2,3);
MessageBox.Show(Heb.GetDayOfYear(tmp));
it's very basic and simple, but yet - i get an errors:
Error 1 The best overloaded method match for System.Windows.Forms.MessageBox.Show(string)' has some invalid arguments..
Error 2 Argument 1: cannot convert from 'int' to 'string'
what is the problem?
I'm not familiar with HebrewCalendar, but given the error message, I'd say that GetDayOfYear is returning an integer.
Try this:
MessageBox.Show(Heb.GetDayOfYear(tmp).ToString());
MessageBox.Show doesn't know how to deal with integers. If you convert it to a string first, it will show you the string representation.