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 15:57:20 GMT From: seas.gwu.edu!mfeldman@uunet.uu.net (Michael Feldman) Subject: Re: putting default value on a subtype of an undefaulted type. Message-ID: <1993May26.155720.24088@seas.gwu.edu> 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. 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 ------------------------------------------------------------------------