iOS HashTable/ArrayList and pragma strict

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?

Thanks

  1. 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.

  2. 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.

–TwisterK

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 :wink: ). “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.

Correct.

–Eric

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).

That will actually still work, although it’s very slow, so avoid doing that. But you will get an error if you try this:

function SomeFunction (myVar) {

–Eric

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.

Just on the iOS type issue again.

If I have an array
var myArray = new Array();

myArray.Add(“Test”);
myArray.Add(44);

What is the correct way to retrieve the values, given that “AS” doesn’t work for value types.

Is it enough to do this?

var myString : String = myArray[0];
var myInt : Int = myArray[1];

Or is some other typecasting required? You can’t use “AS” or parseInt for the int.

Actually, is this correct?

var myStr : String = myArray[0].ToString();
var myInt : int = int.Parse(myArray[1].ToString());

Seems to work with pragma strict, so I assume it should be fine for iOS?

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.

–Eric

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.

It seems likely that it would be more performant, but I haven’t tried it.

–Eric