From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,13b19740d69cbdc2 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-10 08:37:49 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!hammer.uoregon.edu!skates!not-for-mail From: Stephen Leake Newsgroups: comp.lang.ada Subject: Re: Modes (was unbounded_string) Date: 10 Oct 2003 11:18:28 -0400 Organization: NASA Goddard Space Flight Center (skates.gsfc.nasa.gov) Message-ID: References: <20031010074015.761204C40C1@lovelace.ada-france.org> NNTP-Posting-Host: anarres.gsfc.nasa.gov Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: skates.gsfc.nasa.gov 1065799302 17872 128.183.235.92 (10 Oct 2003 15:21:42 GMT) X-Complaints-To: usenet@news.gsfc.nasa.gov NNTP-Posting-Date: 10 Oct 2003 15:21:42 GMT User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 Xref: archiver1.google.com comp.lang.ada:608 Date: 2003-10-10T15:21:42+00:00 List-Id: "Andrew Carroll" 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