[comp.lang.ada] Ada puzzle

bruns@catalina.sw.mcc.com (Glenn Bruns) (09/07/89)

Given the text for an arbitrary Ada procedure, and assuming that all
variables within the procedure are either local variables of the
procedure or parameters of the procedure, is it possible to make a
copy of any variable within the procedure?  By "make a copy", I mean
to create a new variable with the same value as the original variable
that can be used in exactly the same way as the original variable.
Note that no type information is available except as found in the
text of the procedure.

If the variable is a local variable of the procedure, the problem
does not seem too hard.  Consider the following procedure:

    procedure FOO is
	X : INT_ARRAY(1 .. 3) := (4, 5, 6);
	I : INTEGER;
    begin
	I := X(1);  
    end FOO;

This version of FOO contains code to copy X to new variable Y:

    procedure FOO is
	X : INT_ARRAY(1 .. 3) := (4, 5, 6);
	I : INTEGER;
	Y : INT_ARRAY(1 .. 3);
    begin
	Y := X;
	I := Y(1); 
    end FOO;

But what if the variable to be copied is a parameter of the procedure?

    procedure BAR(X : in SOME_TYPE; Z : out SOME_TYPE) is
    begin
	Z := X;
    end BAR;

It does not seem that I know enough about the type of X here to be
able to create a new variable Y that is a copy of X.  The naive
solution is:

    procedure BAR(X : in SOME_TYPE; Z : out SOME_TYPE) is 
	Y : SOME_TYPE;
    begin
	Y := X;
        Z := Y;	
    end BAR;

But this doesn't work when, for example, SOME_TYPE is an
unconstrained array type.  There are ways to copy X if SOME_TYPE _is_
an unconstrained array type, but these techniques don't work for all
types.


-- 
Glenn Bruns 
MCC, Software Technology Program
arpa: bruns@mcc.com    
uucp: {seismo,harvard,gatech,pyramid}!ut-sally!im4u!milano!bruns

hagerp@iuvax.cs.indiana.edu (09/08/89)

How about something like:

	generic
	    type X is (<>);
	procedure Foo (Param : X) is
	    Y : X;
	begin
	    Y := Param;
	end Foo;

Is this what you had in mind?

--paul hager