* Why default values not taken?
@ 2001-06-12 14:53 M. A. Alves
2001-06-12 15:04 ` Tucker Taft
2001-06-12 15:48 ` Jeffrey Carter
0 siblings, 2 replies; 3+ messages in thread
From: M. A. Alves @ 2001-06-12 14:53 UTC (permalink / raw)
To: comp.lang.ada
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?
Thanks,
--
,
M A R I O data miner, LIACC, room 221 tel 351+226078830, ext 121
A M A D O Rua Campo Alegre, 823 fax 351+226003654
A L V E S P-4150 PORTO, Portugal mob 351+939354002
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Why default values not taken?
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
1 sibling, 0 replies; 3+ messages in thread
From: Tucker Taft @ 2001-06-12 15:04 UTC (permalink / raw)
"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?
Aggregates must be complete. Default expressions for record
components are only used for default initializations.
I would recommend you define a "constructor" function
or procedure, with an aggregate inside the subprogram.
You can use defaulted parameters to minimize
the amount of information a given call on the
constructor needs to specify.
In general, there are various good software-engineering
reasons to minimize the use of aggregates outside the package
where a type is defined. This pragmatic issue with defaults
just reinforces the situation.
> Thanks,
>
> --
> ,
> M A R I O data miner, LIACC, room 221 tel 351+226078830, ext 121
> A M A D O Rua Campo Alegre, 823 fax 351+226003654
> A L V E S P-4150 PORTO, Portugal mob 351+939354002
--
-Tucker Taft stt@avercom.net http://www.avercom.net
Chief Technology Officer, AverCom Corporation (A Titan Company)
Burlington, MA USA (AverCom was formerly the Commercial Division of AverStar:
http://www.averstar.com/~stt)
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Why default values not taken?
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
1 sibling, 0 replies; 3+ messages in thread
From: Jeffrey Carter @ 2001-06-12 15:48 UTC (permalink / raw)
"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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2001-06-12 15:48 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox