comp.lang.ada
 help / color / mirror / Atom feed
From: eachus@aries.mitre.org (Robert I. Eachus)
Subject: Re: Stylistic question: returning strings vs. pointers to strings
Date: 7 Mar 90 22:25:39 GMT	[thread overview]
Message-ID: <EACHUS.90Mar7172539@aries.aries.mitre.org> (raw)
In-Reply-To: defaria@hpclapd.HP.COM's message of 6 Mar 90 17:32:59 GMT

In article <920019@hpclapd.HP.COM> Andy DeFaria writes:

>   My question would be why the package mytypes?  Why not:

>   package ARGS is

>     type ARG_PTR is access STRING;

>     function NARGS                return integer;
>     function ARG (index: integer) return ARG_PTR; 

>  end ARGS;

     Why not return a string?  This is not the I/O case where
successive calls (for example to GET_LINE) return different values so:

  package COMMAND_LINE is

    function NARGS                return Integer;
    function ARG (Index: Integer) return String; 

  end COMMAND_LINE;

   This is conceptually much cleaner, and if it is necessary to assign
an argument string to a slice of a fixed length string it can be
easily done:

   Path: String(1..80);
   ...
 begin
   Path(1..Command_Line.Arg(1)'LENGTH) := Command_Line.Arg(1);
   ...

   This looks a little messy, but only because it is fighting the
language, which would perfer that you write:

   Path: constant String := Command_Line.Arg(1);

   this is one of those features/tricks in the language which makes
perfect sense, but only to a compiler writer.  A compiler can easily
allocate a dynamically sized object on the stack, but only if its size
never changes.  And compilers have to be able to handle Ada functions
which return values whose size cannot be determined at compile time,
because certain language primitives such as "&" work that way.  So in
Ada objects (other than records with default descriminant values) must
be constrained, but values and constants need not be.

    If the user needs to use strings designated by pointers, he can
now do it himself:

   type Pointer is access String;
   Path: Pointer;
   ...
 begin
   Path := new String'(Command_Line.Arg(1));
   ...

   This way the package need not export a new type, and need not
depend on a particular library package to provide a pointer type.  The
other alternative would be to make the string pointer type a generic
formal (and the package a generic package, but that would be overkill
in this case.

--

					Robert I. Eachus

with STANDARD_DISCLAIMER;
use  STANDARD_DISCLAIMER;
function MESSAGE (TEXT: in CLEVER_IDEAS) return BETTER_IDEAS is...

  reply	other threads:[~1990-03-07 22:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1990-03-05 23:19 Stylistic question: returning strings vs. pointers to strings Kenneth Almquist
1990-03-06 17:32 ` Andy DeFaria
1990-03-07 22:25   ` Robert I. Eachus [this message]
1990-03-08 14:14     ` Terry J. Westley
1990-03-09  3:16       ` Bryce Bardin
replies disabled

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