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=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!think!linus!eachus From: eachus@aries.mitre.org (Robert I. Eachus) Newsgroups: comp.lang.ada Subject: Re: Stylistic question: returning strings vs. pointers to strings Message-ID: Date: 7 Mar 90 22:25:39 GMT References: <10968@june.cs.washington.edu> <920019@hpclapd.HP.COM> Sender: news@linus.UUCP Organization: The Mitre Corporation, Bedford, MA In-reply-to: defaria@hpclapd.HP.COM's message of 6 Mar 90 17:32:59 GMT List-Id: 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...