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,a1beac047b98caeb X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-06-12 09:04:31 PST Newsgroups: comp.lang.ada Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.skycache.com!Cidera!news-reader.ntrnet.net!uunet!ash.uu.net!xyzzy!nntp From: Jeffrey Carter Subject: Re: Why default values not taken? X-Nntp-Posting-Host: e246420.msc.az.boeing.com Content-Type: text/plain; charset=us-ascii Message-ID: <3B2639EA.BEFDDC30@boeing.com> Sender: nntp@news.boeing.com (Boeing NNTP News Access) Content-Transfer-Encoding: 7bit Organization: The Boeing Company X-Accept-Language: en References: Mime-Version: 1.0 Date: Tue, 12 Jun 2001 15:48:58 GMT X-Mailer: Mozilla 4.5 [en]C-CCK-MCD Boeing Kit (WinNT; U) Xref: archiver1.google.com comp.lang.ada:8614 Date: 2001-06-12T15:48:58+00:00 List-Id: "M. A. Alves" wrote: > > I want to create an object with only some components different from their > default values e.g. > > Contact_Person := new Text_Item'(Remark => US("name and email")); > > where Text_Item inherits Remark from Input_Item and adds Text, both > defaulting to Null_Unbounded_String: > > type Input_Item is tagged record > Remark: Unbounded_String; > end record; > > type Text_Item is new Input_Item with record > Text: Unbounded_String; > end record; > > Of course I get the compile error "no value supplied for component Text". > > But do I really have to supply values for all components? What is the > best idiom here? The reason you get this compilation error is because, yes, you do have to supply values for all components. As for the "best idiom", that can depend on a lot of things. In this case using Null_Unbounded_String for Text is probably fine: (Remark => US ("name and email"), Text => Null_Unbounded_String) If you had specified explicit defaults for the components, things get more interesting. Suppose you wanted the default for Remark to be "Null Remark" and for Text to be "Null Text". You'd probably want to do something like Default_Remark : constant Unbounded_String := To_Unbounded_String ("Null Remark"); Default_Text : constant Unbounded_String := To_Unbounded_String ("Null Text"); type Text_Item is record Remark : Unbounded_String := Default_Remark; Text : Unbounded_String := Default_Text; end record; Then you'd use Default_Text in your aggregate: (Remark => US ("name and email"), Text => Default_Text) I've seen a kludge in which a deferred constant was used for the same effect package P is type Text_Item is record Remark : Unbounded_String := To_Unbounded_String ("Null Remark"); Text : Unbounded_String := To_Unbounded_String ("Null Text"); end record; Null_Text_Item : constant Text_Item; private -- P Default_Text_Item : Text_Item; Null_Text_Item : constant Text_Item := Default_Text_Item; end P; (Remark => US ("name and email"), Text => Null_Text_Item.Text) But I can see no reason to prefer this to the other way, and at least one reason not to prefer it. -- Jeffrey Carter