From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 26 May 93 18:04:36 GMT From: dog.ee.lbl.gov!overload.lbl.gov!agate!howland.reston.ans.net!noc.near.net !inmet!spock!stt@ucbvax.Berkeley.EDU (Tucker Taft) Subject: Re: putting default value on a subtype of an undefaulted type. Message-ID: List-Id: In article groleau@e7sa.crd.ge.com (Wes Groleau X7574) writes: >Situation: > type NO_DEFAULT ( Discrim : NATURAL ) is > record > A : SOME_TYPE; > B : STRING ( 1 .. Discrim ); > end record; > >Problem: > 1. The type declaration is not mine but I'm forced to use it. > 2. Can't declare objects of that type without constraining them > to a fixed value of Discrim. > >Question: > What is the syntax for creating a subtype with the same set of values > as the original type but which has a default discriminant? You can't do this with a subtype declaration. However, Ada 9X allows you to accomplish your goal with a derived type declaration: -- Ada 9X approach: subtype Str_Len is Natural range 0..255; -- or whatever is reasonable type With_Default(Len : Str_Len := 0) is new No_Default(Discrim => Len); . . . X : With_Default; -- X.Len initially zero, but can become -- larger as part of a whole-object assignment You can then use explicit conversion back to No_Default when necessary, though many of the operations you want will probably be inherited automatically by With_Default anyway, thanks to the normal rules for derived types. >Curiosity: > What is the syntax for giving A a default value in a subtype? You can't, but you can (in Ada 9X) when defining a derived type. Alas, you may have to wait a while before this capability is available in your local Ada compiler. However, in the mean time, you can accomplish roughly the same thing by wrapping No_Default in a record with a default, as follows: -- Ada 83 approach: type With_Default(Len : Str_Len := 0) is record Data : No_Default(Discrim => Len); end record; . . . X : With_Default; -- X.Len initially zero, but can become -- larger as part of a whole-object assignment You can then use X.Data to get a value of type No_Default, and use "With_Default'(Data => ND)" to construct a value of type With_Default given ND of type No_Default. No subprograms are inherited automatically by With_Default, since it is a wrapper for, rather than a derivative of, No_Default. But you can write your own wrapper subprograms straightforwardly. Hope that helps... S. Tucker Taft stt@inmet.com Ada 9X Mapping/Revision Team Intermetrics, Inc. Cambridge, MA 02138