Modifiying the Contents of Limited Types Passed as "in" Parameters


The Contents of Limited Types Passed as "in" Parameters
can be modified using the following technique:

with Ada.Text_IO; use Ada.Text_IO;

procedure Rosen_Trick is

   type T;

   type Relay_Type (Reference : access T) is limited null record;

   type T is limited
      record
         Relay : Relay_Type (T'Access);
         Data  : Natural := 0;
      end record;

   function Next (X : in T) return Integer is
      Data : Natural renames X.Relay.Reference.Data;
   begin
      Data := Data+1;
      return Data;
   end Next;

   Test_T : T;

begin

   for N in 1 .. 10 loop
      Put_Line(Integer'Image(Next(Test_T)));
   end loop;

end Rosen_Trick;

Contributed by: Jean-Pierre Rosen
Contributed on: September 17, 1999
License: Public Domain

Back