with Ada.Text_IO;         use  Ada.Text_IO;
with Ada.Integer_Text_IO; use  Ada.Integer_Text_IO;

-- This example uses slices and unconstrained arrays
procedure slicedemo2 is

    Type MyArray is array (Positive Range <>) of Integer;

    procedure putArray(theArray: MyArray) is
    begin
        for i in theArray'first .. theArray'last loop
            put(theArray(i), width => 4);
        end loop;
        new_line;
    end putArray;

    -- Create 2 arrays of different size
    a: MyArray := (11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
    b: MyArray := (81, 82, 83, 84, 85, 86, 87, 88 );

    c: MyArray(a'first .. a'last + 1);

    m, n: Integer;
begin
    putArray(c);
    c := a & (21);  -- Make sizes same
    c(1) := 999;
    putArray(a);
    putArray(c);

    putArray(a);
    putArray(b);
    putArray(b(2 .. 5));

    m := 3;
    n := 5;
    putArray(a(m .. n));
    putArray(a(n .. m));

    -- Assign a slice with an aggregate value
    a(2 .. 4) := (-1, -2, -3);
    -- Warning of runtime error if aggregate is wrong size

    -- Now let's see what is in a
    putArray(a);

    -- Assign one slice to another
    a(1 .. 3) := a(8 .. 10);          

    putArray(a);

    -- When slices overlap, the values shift
    a(1 .. 9) := a(2 .. 10);          

    putArray(a);

    -- Types on both sides of assignment must be identical

    -- a(1 .. 3) := b;        -- Will compile but warns of runtime error
    a(1 .. 8) := b;          -- Okay
    a(1 .. 3) := b(2 .. 4);  -- Okay

    putArray(a);

end slicedemo2;
-- Output:
--|-1209014912-1077699528-1210003044-1209014912   0   0-1210394320134727936-1208877856   0-1077699496
--|  11  12  13  14  15  16  17  18  19  20
--| 999  12  13  14  15  16  17  18  19  20  21
--|  11  12  13  14  15  16  17  18  19  20
--|  81  82  83  84  85  86  87  88
--|  82  83  84  85
--|  13  14  15
--|
--|  11  -1  -2  -3  15  16  17  18  19  20
--|  18  19  20  -3  15  16  17  18  19  20
--|  19  20  -3  15  16  17  18  19  20  20
--|  82  83  84  84  85  86  87  88  20  20