comp.lang.ada
 help / color / mirror / Atom feed
* What does this statement do?
@ 2001-11-14 23:29 Adam Beneschan
  2001-11-15  7:49 ` Preben Randhol
  2001-11-15 15:32 ` Matthew Heaney
  0 siblings, 2 replies; 4+ messages in thread
From: Adam Beneschan @ 2001-11-14 23:29 UTC (permalink / raw)


Question for Ada gurus: In the following program, what should happen
with the statement marked HERE?

(a) Z is set to the value of the local variable N
(b) Func is called recursively, and Z is set to the component N of the
    function result
(c) The compiler rejects the statement as ambiguous

				-- thanks, Adam

with Text_IO; use Text_IO;
procedure Test is
    
    type Rectype is record
        M : integer;
        N : integer;
    end record;
    
    Result    : Rectype;
    Recursive : boolean := false;
    
    function Func return Rectype is
        
        N : integer;
        X : integer;
        
        procedure Inner (Z : out integer) is
        begin
            Z := Func.N;        -- <======== HERE
        end Inner;
        
    begin
        N := 1;
        if Recursive then
            return (M => 2, N => 3);
        else
            Recursive := true;
            Inner (X);
            return (M => X, N => X);
        end if;
    end Func;
    
begin
    Result := Func;
    Put_Line (integer'image (Result.M) & ", " & integer'image
(Result.N));
end Test;



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: What does this statement do?
  2001-11-14 23:29 What does this statement do? Adam Beneschan
@ 2001-11-15  7:49 ` Preben Randhol
  2001-11-15 15:32 ` Matthew Heaney
  1 sibling, 0 replies; 4+ messages in thread
From: Preben Randhol @ 2001-11-15  7:49 UTC (permalink / raw)


On 14 Nov 2001 15:29:01 -0800, Adam Beneschan wrote:
> Question for Ada gurus: In the following program, what should happen
> with the statement marked HERE?

[Cut homework]

Read chapter 5 of

http://burks.bton.ac.uk/burks/language/ada/ada95.pdf

Preben
-- 
             Nothing is fool-proof to a sufficiently talented fool.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: What does this statement do?
@ 2001-11-15 13:45 Gautier Write-only-address
  0 siblings, 0 replies; 4+ messages in thread
From: Gautier Write-only-address @ 2001-11-15 13:45 UTC (permalink / raw)
  To: comp.lang.ada

>Question for Ada gurus: In the following program, what should happen
>with the statement marked HERE?

>(a) Z is set to the value of the local variable N
>(b) Func is called recursively, and Z is set to the component N of the
>     function result
>(c) The compiler rejects the statement as ambiguous
>
>				-- thanks, Adam

Are you sure there is a possible ambiguity ? You have the right
to change the name of one 'N' to check this! Try compiling it
(e.g. with the ICC compiler...).

____________________________________________________________
Gautier  --  http://www.mysunrise.ch/users/gdm/index.htm#Ada

NB: Do not answer to sender address, visit the Web site!
    Ne r�pondez pas � l'exp�diteur, visitez le site ouaibe!


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: What does this statement do?
  2001-11-14 23:29 What does this statement do? Adam Beneschan
  2001-11-15  7:49 ` Preben Randhol
@ 2001-11-15 15:32 ` Matthew Heaney
  1 sibling, 0 replies; 4+ messages in thread
From: Matthew Heaney @ 2001-11-15 15:32 UTC (permalink / raw)



"Adam Beneschan" <adam@irvine.com> wrote in message
news:b4682ab7.0111141529.3598c481@posting.google.com...
> Question for Ada gurus: In the following program, what should happen
> with the statement marked HERE?
> (c) The compiler rejects the statement as ambiguous

What did you expect to happen?  The name N can mean the N component of the
Rectype object returned by Func, or it can mean the local variable N.  The
compiler is correct.

Applying the selection operator to a function return has a long tradition in
Ada; this is an especially important idiom since Ada doesn't have explicit
reference types a la C++.

For example, this is how you implement a stack (or whatever) whose items are
limited:

generic
   type Item_Type is limited private;
package Stacks is
   type Stack_Type is limited private;

   type Item_Access is access all Item_Type;
   for Item_Access'Storage_Size use 0;

   procedure Push (Stack : in out Stack_Type);

   function Get_Top (Stack : access Stack_Type) return Item_Access;
...
end;

You'd use it like this:

declare
   S : aliased Stack_Type;
begin
   Push (S);

   declare
      Item : Some_Limited_Type renames Get_Top (S'Access).all;
   begin
      Do_Something (Item);
   end;
end;

I few days ago I used a similar technique that showed how to implement
Java-style interfaces in Ada95.







^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2001-11-15 15:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-14 23:29 What does this statement do? Adam Beneschan
2001-11-15  7:49 ` Preben Randhol
2001-11-15 15:32 ` Matthew Heaney
  -- strict thread matches above, loose matches on Subject: below --
2001-11-15 13:45 Gautier Write-only-address

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox