If I wanted to declare a static compile-time array of Byte, I could do it like this:
var
bytes :array[0..24] of Byte = (1, 2, 3, .... );
However, the type of that is array[0..24] of byte, not System.TArray<System.Byte>, which is more commonly known as TBytes.
What I need is something that can be of type TBytes but I don’t want to have to add an initialization section to hold these byte values in some painful way:
var
bytes2:TBytes;
initialization
SetLength(bytes2,24);
bytes2[0] := 1; bytes2[1] := 2; ....
Is there some way to do this instead:
var
bytes2:TBytes = (1,2,3, .... );
I tried also to find a way to convert quickly from TBytes (System.TArray<System.Byte>) and array[0..24] of Byte, like this:
bytes2 := byte;
Unfortunately, the closest I can get is this brute-force code:
SetLength(bytes2,Length(bytes));
for n := 0 to Length(bytes) do begin
bytes2[n] := bytes[n];
end;
It seems to me for two types so closely related, that the compiler could do a little better job of allowing me to coerce or copy, from the one type to the other. Anybody else feel that way about various types of “Array of X”? Know any cool ways around it? If the compiler did some magic, it might make the Move(…) function work for this case, but Move actually gives you an access violation, and can’t be used with dynamic arrays or generic collections.
How about:
That said I always find it limiting that this syntax does not accept open arrays. So I have a bunch of functions that look like this:
I believe that the various generics enhancements in XE mean that this could be done with generics and without duplicating routines like
Bytesfor each different scalar.