Hey guys, just a few questions relating to strong typing
when I have #pragma strict at the top of my JavaScript script, shouldn’t it cause an error when I declare a variable like var myInt = 5; ? Shouldn’t it force me to use var myInt : Int = 5;?
To my understanding, iOS requires everything to be strong typed. Does this mean you can’t use JavaScript-Arrays, ArrayLists or HashTables, since they are untyped and can contain mixed type values?
Or… does it mean you can use ArrayLists/HashTables and you can store mix typed variables, but when retrieving those values you must typecast them?
When we hav pragma strict, all the variables need to b declare it’s type and stick wif it until the program quit, so it is ok to declare var myInt = 5, since you already declare it as int and it will be int until the program quit, thus no error shown.
You still can use ArrayList, Javascript Array and what so ever, if you’re using the Javascript array which stored object as array, u need to type cast when retrieve object from the array.
No, when you say “var myInt = 5”, type inference makes myInt an int regardless of whether you explicitly specify it or not. If you used “var myInt = 5.0”, then myInt would be a float (and badly named ). “var myInt = 5.0d” would make myInt a double. And so on. This isn’t dynamic typing, because the type is set when the script is compiled and can’t be changed at runtime.
Okay thanks for the clarification on type inference
Although I’m sure I also declared a variable without a type and still didn’t get an error, for example
var myInt; // no type and no initial value
// use it later in code
function SomeFunction()
{
myInt = 5;
}
Will type inference still work like that, or should that throw an error since the type wasn’t declared/inferred when it was created? Or is the complier smart enough to figure out that some function uses it as an int, so it must be an int (and it only becomes Dynamic if one function assigns and int to it, and another assigns a string).
Okay thanks, I try and specify type wherever possible, however I’m trying to think of a good way to handle the data I’m retrieving from a database. In PHP I would normally use an associative array, but it’s a weak typed language so I may need to approach it differently here.
I was thinking perhaps an array of hashtables, each hashtable corresponding to one of the row results. It’s a one off operation so it doesn’t need to be super efficient.
No, “var myInt : int = myArray[0];” and so on is fine. With #pragma strict you get a warning about implicit downcasts when doing that, so you can use #pragma downcast to make the warning go away. However, it would still be a good idea not to use the JS Array class. For a heterogenous array, use List.(). It has the same issue of needing to cast variables when retrieving values, but it’s almost twice as fast and has more abilities.
Oh okay didn’t realise you could use List like that. I’ll actually be using a Hashtable because I need to retrieve by key value, so a List isn’t appropriate.
I suppose I could use a Dictionary instead of a Hashtable with Dictionary.<String, Object) then? Would it be worth doing that instead of Hashtables?
It’s also only used for fairly rare calls (storing results of a server WWW request or database request for example), so performance isn’t really a concern. Everywhere else I pretty much use built-in arrays.