comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <jeffrey.carter@boeing.com>
Subject: Re: Why default values not taken?
Date: Tue, 12 Jun 2001 15:48:58 GMT
Date: 2001-06-12T15:48:58+00:00	[thread overview]
Message-ID: <3B2639EA.BEFDDC30@boeing.com> (raw)
In-Reply-To: mailman.992354057.29715.comp.lang.ada@ada.eu.org

"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



      parent reply	other threads:[~2001-06-12 15:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-12 14:53 Why default values not taken? M. A. Alves
2001-06-12 15:04 ` Tucker Taft
2001-06-12 15:48 ` Jeffrey Carter [this message]
replies disabled

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