I gather this might be unknown to some, so heres a demonstration of how to use arrayrefs to pass arrays to functions. The trick is to use typedefs to mask the array.
typedefint[2]array_int_2;array_int_2array;// could be int[2] array; as wellvoidTest(arrayref<array_int_2>instance){instance[0]=instance[0]-1;if(instance[0]>=0){Test(array);}}voidInit(){array[0]=50;// makes Test call itself 50 timesTest(array);// after this, array[0] should be == -1}
Notes:
arrayrefs check the size of arrays. You cant pass arrays of different sizes to arrayrefs.
You cant use non-array variable types for arrayrefs.
Notes on other reference types:
funcrefs cant be used as an array when the return-type of the prototype is void (JademusSreg discovered this).
structrefs and arrayrefs cant be used outside local variables and parameters (as the editor will happily inform you).
The implicit type casting rule in galaxy 1.5 has been changed a lot -> toward good direction.
Still I'm wondering if using the typedef to mask the type is the "official" way to create arrayref, it seems... very unnecessary.. and code wasting... Maybe only blizzard could answer this question, at least this method works, so we can use it for now.
In fact, in my intuition. If the typedef way is the official way, then it should also allow us to use the arrayref as a return type of functions, but it doesn't. So I'm still not sure.
Typedef seems to be the only way. I suspect its an error in their grammar for galaxy, ie. the grammar only allows Identifiers to be used inside arrayref<>, and not Identifiers + Array Dimension sizes. It might not be an error, but rather a deliberate omission.
It makes sense that structrefs and arrayrefs are not allowed to be returned, considering that there is no dynamic allocation support and considering that local variables go out of scope when the function has been ran through.
There's still possibilities to return arrayrefs. The passed parameter can be modified, and as such, the called constructs the array and passes it to the function to have it filled (but I assume you know of this already, oh well).
@Mille
I recall it for now isn't possible to do so. But I can imagine that it will be implemented either now in the beta or when the patch is released on the retail game version.
I'd just like to add that when using function references, the reference type (within the <>) appears to define the template prototype that the function we are trying to reference must have, as this was not very clear to me until I tried out some examples.
Of the examples above, 2 would fail because the first parameters is different, 3 would fail because the return type is different. Feel free to correct me if i'm mistaken. This results from above are part test/part theory.
I gather this might be unknown to some, so heres a demonstration of how to use arrayrefs to pass arrays to functions. The trick is to use typedefs to mask the array.
Notes:
Notes on other reference types:
No need to use "array_int_2 array"
You can use "int[2] array"
and it would still works.
The implicit type casting rule in galaxy 1.5 has been changed a lot -> toward good direction.
Still I'm wondering if using the typedef to mask the type is the "official" way to create arrayref, it seems... very unnecessary.. and code wasting... Maybe only blizzard could answer this question, at least this method works, so we can use it for now.
In fact, in my intuition. If the typedef way is the official way, then it should also allow us to use the arrayref as a return type of functions, but it doesn't. So I'm still not sure.
Is there any way to use this feature with GUI? I cant find anything that seems matching.
Would be really helpful, especially since GUI is pretty much the only officially supported programming interface by blizzard.
@Renee2islga: Go
Typedef seems to be the only way. I suspect its an error in their grammar for galaxy, ie. the grammar only allows Identifiers to be used inside arrayref<>, and not Identifiers + Array Dimension sizes. It might not be an error, but rather a deliberate omission.
@Renee2islga: Go
It makes sense that structrefs and arrayrefs are not allowed to be returned, considering that there is no dynamic allocation support and considering that local variables go out of scope when the function has been ran through. There's still possibilities to return arrayrefs. The passed parameter can be modified, and as such, the called constructs the array and passes it to the function to have it filled (but I assume you know of this already, oh well).
@Mille
I recall it for now isn't possible to do so. But I can imagine that it will be implemented either now in the beta or when the patch is released on the retail game version.
And do you sure the void functions can't be array? Or I just misunderstood this english sentence?(my english isn't good.)
It works for me when main() returns void.
funcref<main>[2] aFuncRefArray;
aFuncRefArray[0]=main;
Yea, of course. :)
Some other intesting things about the arrayref
The int2x2 and int2D are actually the same:
But, have you ever tried this?
Result: testInt2[0]=1;test2dArray[0][0]=1;
Very interesting indeed~
And as you can see in #8.
You can actually use global arrayref and structref - just put them into a global array :)
I believe it is a bug in the galaxy syntax. But I would rather hope they won't fix it. It is very useful.
Without it, all the fun things in #8 cannot be done.
I'd just like to add that when using function references, the reference type (within the <>) appears to define the
prototype that the function we are trying to reference must have, as this was not very clear to me until I tried out some examples.templateSo for example:
Of the examples above, 2 would fail because the first parameters is different, 3 would fail because the return type is different. Feel free to correct me if i'm mistaken. This results from above are part test/part theory.
Yea, the prototype need to be the same. The parameters and the return type.
Unfortunately, they "fixed" the "global arrayref array" in 1.5 beta patch 3. So we cannot do that now :(
http://www.sc2mapster.com/forums/general/general-chat/39702-so-they-fixed-the-global-arrayref-array-1-5-patch-3/