comp.lang.ada
 help / color / mirror / Atom feed
* putting default value on a subtype of an undefaulted type.
@ 1993-05-26 15:19 Wes Groleau X7574
  0 siblings, 0 replies; 4+ messages in thread
From: Wes Groleau X7574 @ 1993-05-26 15:19 UTC (permalink / raw)


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?  

Curiosity:
      What is the syntax for giving A a default value in a subtype? 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: putting default value on a subtype of an undefaulted type.
@ 1993-05-26 15:57 Michael Feldman
  0 siblings, 0 replies; 4+ messages in thread
From: Michael Feldman @ 1993-05-26 15:57 UTC (permalink / 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.

You better ask whoever wrote that type declaration whether he REALLY
expects that the string could end of being Natural'Last characters
long, or whether the type of the discriminant should be set a tad
more realistically. On a compiler with 32-bit predefined Integer,
Natural'Last is a pretty big number...

This is almost an FAQ and comes up every coupla months here.

The only redemption in this really naive use of the type system is 
_precisely_ that all objects will have to be constrained to a fixed
value of Discrim. This means that the object is stuck with whatever
size you plug into the discriminant. No problem here.

If you plugged in a default, leaving the objects unconstrained, 
you'd be at the mercy of your compiler's allocation scheme for 
these li'l suckers.

Many compiler authors, ever responsive to their users' demand for 
execution speed, avoid reallocating unconstrained objects when their
logical sizes change. How? They allocate the max. No free lunch.
You want speed? Don't reallocate. You want no reallocation? Allocate
the max.

What's the max in your case? Natural'Last. Try it on your friendly
Verdix or TeleSoft compiler. See what happens when you try to declare
an object of that type and an attempt is made to allocate a gig or
so of memory.

Now try it with Meridian. Nooooooo problem. Why? They just allocate
a header block, then acquire heap space as needed for the data.
Much more economical of space, but costly in time. No free lunch.
>
>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?  
I don't think you can do it; nothing comes to mind. You can certainly
_constrain_ a subtype, but that isn't what you want. You can make
a constrained subtype of the unconstrained version, but I doubt if
you can do it the other way round. Even if you could, you shouldn't.

What a lotta people don't realize - because of the closeness of the
two syntaxes, is that constrained variant records (no default) and
unconstrained variant records (default supplied) are really two
VERY different data structures. 

The first is a static structure: you pin down the size at object 
declaration. The second is a dynamic structure: the size can vary 
during the life of the object. These leads to entirely different 
allocation schemes; studying the various vendors' strategies is 
actually a mini-data structures course. I gave you 2 examples.
(Linked lists are even sometimes used!)

Mixing the two kinds of data structures is, IMHO, a dangerously
misleading program architecture. The purpose of the unconstrained
version is to support not knowing the size till after you get the 
data. If that is your situation, you ought to define a whole
new type for it, not try to glom onto the constrained version,
which presumes essentially that you can pin the size down statically.
You say you're "stuck" with it - try to get unstuck. You'll feel
better in the end.

And if you're going to use an unconstrained version, give yourself
at least a shot at portability by putting a realistic, application-
dependent, bound on the discriminant range. Better yet, put your
type definition in a generic, then instantiate for the range you need
in a given application. This seems to be an increasingly popular
way to implement dynamic string packages.
>
>Curiosity:
>      What is the syntax for giving A a default value in a subtype? 
Again, I think that by being stuck with the type, you're stuck.
Nothing comes to mind, but I could be wrong here.

Mike Feldman
------------------------------------------------------------------------
Michael B. Feldman
co-chair, SIGAda Education Committee

Professor, Dept. of Electrical Engineering and Computer Science
School of Engineering and Applied Science
The George Washington University
Washington, DC 20052 USA
(202) 994-5253 (voice)
(202) 994-5296 (fax)
mfeldman@seas.gwu.edu (Internet)

"The most important thing is to be sincere, 
and once you've learned how to fake that, you've got it made." 
-- old show-business adage
------------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: putting default value on a subtype of an undefaulted type.
@ 1993-05-26 18:04 dog.ee.lbl.gov!overload.lbl.gov!agate!howland.reston.ans.net!noc.near.net
  0 siblings, 0 replies; 4+ messages in thread
From: dog.ee.lbl.gov!overload.lbl.gov!agate!howland.reston.ans.net!noc.near.net @ 1993-05-26 18:04 UTC (permalink / 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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: putting default value on a subtype of an undefaulted type.
@ 1993-05-26 19:29 Robert I. Eachus
  0 siblings, 0 replies; 4+ messages in thread
From: Robert I. Eachus @ 1993-05-26 19:29 UTC (permalink / raw)


In article <C7n3wB.BGJ@crdnns.crd.ge.com> groleau@e7sa.crd.ge.com (Wes Groleau 
X7574) asks:

 >  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.  Records such as this (but with defaults) are a very
magic animal in Ada, and will almost certainly be represented in a
very different way.  Alternatives are:

     1) Create your own varying type, and call explict conversion
functions where necessary.

     2) Use declare blocks where you need to create objects.  (The
size of objects need not be static, just set when the object is
created.)

     3) Go beat on the supplier of the type to provide a type with a
default.


 >  Curiosity:
 >	 What is the syntax for giving A a default value in a subtype? 

    Again, not a supported feature.

--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1993-05-26 19:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1993-05-26 15:19 putting default value on a subtype of an undefaulted type Wes Groleau X7574
  -- strict thread matches above, loose matches on Subject: below --
1993-05-26 15:57 Michael Feldman
1993-05-26 18:04 dog.ee.lbl.gov!overload.lbl.gov!agate!howland.reston.ans.net!noc.near.net
1993-05-26 19:29 Robert I. Eachus

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