comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <Stephe.Leake@nasa.gov>
Subject: Re: Modes (was unbounded_string)
Date: 10 Oct 2003 11:18:28 -0400
Date: 2003-10-10T15:21:42+00:00	[thread overview]
Message-ID: <uzng9f7ej.fsf@nasa.gov> (raw)
In-Reply-To: mailman.60.1065773352.25614.comp.lang.ada@ada-france.org

"Andrew Carroll" <andrew@carroll-tech.net> writes:

> >>For all other types, the compiler chooses the parameter 
> >>mode best suited to the type. 
> Great!!!  SIGN ME UP!!!  But...is it possible that the compiler 
> doesn't get it right?  

What does "right" mean here? Part of the point is that most of the
time it does not matter whether things are passed by copy or
reference. If you right code where it does matter, that code is
erroneous. Note that the same code would be erroneous in any other
language.

Tagged types and full limited types are always passed by reference,
because it almost always matters for them.

> What would I need to do if I found that the compiler didn't get it
> right in my testing? I'm guessing that you will say, "specify the
> mode".

You can't specify the parameter passing mechanism, except by making
the type tagged or fully limited.

> package x is
> type x;
> type xptr is access x;
> type x is tagged limited record
>  head: nodeptr; 
>  something: unbounded_string;
> end record;
> 
> --creates a new node to put into the 'head' list.
> --new node is created from information in token.
> --token has nothing to do with the 'something' variable
> --declared in the record above.
> procedure add(this: xptr; token: unbounded_string); 
> 
> end x;
> 
> So, with the above code "example", xptr is passed as an 'in'
> or 'in out' mode at the add procedure?  

Ah. You are confused. Let me back up.

If you do not specify "in" or "in out", the mode is "in". 

But that is _not_ what we meant by "let the compiler choose". We were
talking about "by reference" vs "by copy". 

"in" does _not_ mean "by copy". It just means "a constant view; you
can't assign to it". It may be passed by copy or by reference.
Similarly, "in out" does _not_ mean "by reference"; it just means "a
variable view; you can assign to it". It my also be passed by copy;
the compiler writes the local copy back to the calling copy on
subprogram return.

> Are you saying that I should use just x and not xptr as the
> parameter so that the compiler will choose the correct mode? 

Almost. You should use X, but specify "in" or "in out". The compiler
will decide whether to pass by reference or by copy.

> If so, you recommend it because of deallocation issues?

Yes. And general clarity; using pointers usually means you are doing
dynamic memory allocation, and/or dynamic dispatching. If you are not,
don't use pointers.

> Is there something in the code below that I missed about information 
> hiding?  
> 
> Temp: gnat.os_lib.String_Access;
> ...
> Temp := gnat.os_lib.getenv("QUERY_STRING");
> ...
> ---------------------------------------
> -- ready to use Temp for something else
> ---------------------------------------
> Temp := gnat.os_lib.getenv("SOME_OTHER_VARIABLE");
> 

Since you are using environment variables, this is the correct idiom;
environment variables are not an Ada thing, you have to use whatever
the compiler provides.

> Considering the code above, what is the "Ada idiom" to "resize" a String
> without pointers and dynamic allocation?  

Note that you did _not_ "resize" anything. gnat.os_lib.getenv returns
a pointer to a string, not a string. On the second call, you get
another pointer. Both environment strings are still there, unmodified.

-- 
-- Stephe



  parent reply	other threads:[~2003-10-10 15:18 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20031010074015.761204C40C1@lovelace.ada-france.org>
2003-10-10  8:27 ` Modes (was unbounded_string) Andrew Carroll
2003-10-10  9:15   ` Dmitry A. Kazakov
2003-10-11  7:16     ` Simon Wright
2003-10-13  9:28       ` Dmitry A. Kazakov
2003-10-10 15:18   ` Stephen Leake [this message]
2003-10-10 16:21     ` Martin Dowie
2003-10-14 18:47     ` Craig Carey
2003-10-10 19:06   ` Jeffrey Carter
2003-10-13  9:33     ` Dmitry A. Kazakov
2003-10-13  9:40       ` Stephane Richard
2003-10-13 10:12         ` Dmitry A. Kazakov
2003-10-15  2:16         ` Warren W. Gay VE3WWG
2003-10-15  3:36           ` Jeff C,
2003-10-16 16:45             ` Warren W. Gay VE3WWG
     [not found] <20031010094017.680474C40C1@lovelace.ada-france.org>
2003-10-10 10:58 ` Andrew Carroll
replies disabled

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