I tried to convert the function A
to the new Ada 2022 reduction expression, but I get a range check failed
error. What am I doing wrong?
with Ada.Text_IO;
with Ada.Unchecked_Conversion;
with Interfaces;
procedure Learn is
type u8 is new Interfaces.Unsigned_8;
type u32 is new Interfaces.Unsigned_32;
type Bytes is array (Positive range <>) of aliased u8;
type Words is array (Positive range <>) of aliased u32;
Some_Words : Words (1 .. 3) := [1111, 22222, 333333];
subtype Bytes_4 is Bytes (1 .. 4);
function Convert is new Ada.Unchecked_Conversion (u32, Bytes_4);
function A (W : Words) return Bytes is
(Convert (W (1)) & Convert (W (2)) & Convert (W (3)));
A1 : Bytes := A (Some_Words);
function B (W : Words) return Bytes is
([for X of W => Convert (X)]'Reduce ("&", []));
B1 : Bytes := B (Some_Words);
begin
Ada.Text_IO.Put_Line (A1'Image);
Ada.Text_IO.Put_Line (B1'Image);
end Learn;
Console output:
$ gprbuild -q -P main.gpr
learn.adb:22:30: warning: too many elements for subtype of "Bytes" defined at line 22 [enabled by default]
learn.adb:22:30: warning: expected 0 elements; found 4 elements [enabled by default]
learn.adb:22:30: warning: Constraint_Error will be raised at run time [enabled by default]
Build completed successfully.
$ ./learn
raised CONSTRAINT_ERROR : learn.adb:22 range check failed
exit status: 1