comp.lang.ada
 help / color / mirror / Atom feed
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.
Date: 26 May 93 18:04:36 GMT	[thread overview]
Message-ID: <C7nBJp.G9G@inmet.camb.inmet.com> (raw)

In article <C7n3wB.BGJ@crdnns.crd.ge.com> 
  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

             reply	other threads:[~1993-05-26 18:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-05-26 18:04 dog.ee.lbl.gov!overload.lbl.gov!agate!howland.reston.ans.net!noc.near.net [this message]
  -- strict thread matches above, loose matches on Subject: below --
1993-05-26 19:29 putting default value on a subtype of an undefaulted type Robert I. Eachus
1993-05-26 15:57 Michael Feldman
1993-05-26 15:19 Wes Groleau X7574
replies disabled

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