Array initialisation

BlitzMax Forums/BlitzMax Programming/Array initialisation

Vlad(Posted 2007) [#1]
Global array1:Short[] = [1,2,3]
Global array2:Byte[] = [1,2,3]

Compiler error: "Unable convert 'int array' to 'short/byte array'"


Dreamora(Posted 2007) [#2]
put :byte and :short behind them.
what you have there are int arrays.


Vlad(Posted 2007) [#3]
Please post a working sample(if it's really exist) this is not big source.
BTW
Global array1:Short[] = Short[1,2,3]
'or
Global array1:Short[] = [1,2,3]:Short
- not right.
And direct typecasting for each element is so weird.


Vlad(Posted 2007) [#4]
Oh, Dreamora, you right, but this is direct typecasting. I use it other form as 'Byte(...)/short(...)' for each element. But this is not a good idea - i needs a huge array and write type for each element is very very bad.


Dreamora(Posted 2007) [#5]
Both is wrong, seems like you haven't used direct type declaration on numerics so:

global array1:short[]= [1:short, 2:short, 3:short]



Vlad(Posted 2007) [#6]
I understand you, but direct typecasting for each element is not a good idea if we have a huge array.
BTW this is a bit stupid - declare array as short and declare type individually to each element of array.
May be this songs as 'feature request', but i think this is compiler bug or miss.


Dreamora(Posted 2007) [#7]
Initializing an array that way is a bad idea anyway.
It is slow, 3-10 times slower than

global array1:short[] = new short[3]
array1[0] = 1
array1[1] = 2
array1[2] = 3


and the problem gets larger the larger the array is you assign.
You know, arrays in a managed environment are a little a different thing than in C++, where they are just a continous block of memory that you can create with memalloc and its done. (in BM an array of any types are extended of the class ARRAY actually)


marksibly(Posted 2007) [#8]
This is by design.


Dreamora(Posted 2007) [#9]
Thanks