Next: , Previous: , Up: Arrays   [Contents][Index]


20.4.3 Casting Arrays

There are two kinds of casts for array values:

For example, we have seen that the type of an array literal is always bounded by a constant number of elements. Thus the type of [1,2,3] is int[3].

We could cast the value above to an unbounded array type by doing:

[1,2,3] as int[]

Or to an array bounded by size:

[1,2,3] as int[12#B]

Of course trying to cast an array value to a type for which the bounding restrictions will fail. This will give you a compile-time error:

[1,2,3] as int[4]

And this will result in a run-time error:

(poke) [1,2,3] as int[13#B]
unhandled conversion error exception

Now let’s see how Poke reinterprets integral arrays as integers:

(poke) [0xdeUB,0xadUB] as uint16
0xdeadUH
(poke) [0xdeUB,0xadUB,0UB] as int
0xdead00

Trying to cast an array with a size larger than 64-bit will result in an error:

(poke) [1,2,3] as int
unhandled conversion error exception
arrays bigger than 64-bit cannot be casted to integral types

It also works for nested arrays and arrays of integral structs (See Integral Structs):

(poke) type Foo = struct uint { int16 hi; int16 lo; };
(poke) [[Foo{ hi=0xdeadH, lo=0xbeefH}],[Foo{}]] as ulong
0xdeadbeef00000000UL

The way it works is like putting all array elements in an uint<64> number and then casting that number to the destination integral type.


Next: , Previous: , Up: Arrays   [Contents][Index]