From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,6501d9f1c65bd0d1 X-Google-Attributes: gid103376,public From: Thomas.Kendelbacher@erno.de (Thomas Kendelbacher) Subject: Re: Ada 83: Type defaults Date: 1996/09/11 Message-ID: <516rqo$e4h@mailsrv2.erno.de>#1/1 X-Deja-AN: 179980671 references: <516mkh$d3m@ryker.dma.gov> organization: Daimler-Benz Aerospace, Space Infrastructure reply-to: Thomas.Kendelbacher@erno.de newsgroups: comp.lang.ada Date: 1996-09-11T00:00:00+00:00 List-Id: In article <516mkh$d3m@ryker.dma.gov>, hehmeyerj writes: >I have an enumeration type declaration. For example: > >type x is (A, B, C, D); > >And I have about three hundred variables of type x scattered throughout >50 packages. How would you default the type to, say A, without going >to each individual variable declaration and explicitly defaulting them. >(ie y : x := A;) (Almost) no chance, the only way to associate default values with a type declaration is for record fields (and the default "null" for access types, of course.) I.e. (attention! hack following!) you could replace your original type x by type field_type is (A_val, B_val, C_val, D_val); type x is record val: field_type := A_val; end record; A : constant x := x'(val => A_val); B : constant x := x'(val => B_val); C : constant x := x'(val => C_val); D : constant x := x'(val => D_val); This would implicitly initialize all variables of type x to A, without changing their declarations. Assignments like "y := A" would still be valid because of the constant declarations, comparisons using "=", "/=" would continue to work as expected, too; memory consumption and performance would be about the same. You can make "<", ">" etc. work by declaring operator functions; again, you could make memory usage and performance the same by inlining them. Things that will break are uses of the enumeration type attributes, like x'POS, x'VAL, x'IMAGE, since these are not and cannot be defined for record types (you could define own functions, of course, but the call syntax would be different), and obviously any attempt to instantiate ENUMERATION_IO with x (again, you can make your own I/O operations mimicking the ENUMERATION_IO interface.) Other things that won't work: Use of x-typed expressions as case expressions, as array indices or as "for" loop ranges, as all these require discrete types. (For these uses, you would have to replace "y" of type x by "y.val" and "A" by "A_val" in your code, returning to the underlying enumeration type.) But don't worry, the compiler will find all these cases for you... ;-) Only you can tell whether this is easier than finding and changing 300 declarations manually (with the risk of missing the one for which it would have been essential :-); this depends on your program. No need to say, but I would recommend a solution as that outlined above only if for some very good reason you *must* ensure that every variable of type x is initialized to the same value. In any other case, this would seem a bit too obscure. Not generally recommended. -- Thomas Kendelbacher | email : Thomas.Kendelbacher@erno.de DASA RI / Abt. RIT14 | voice : +49 421 539 5492 (working hours) Postfach 28 61 56 | or : +49 421 57 04 37 (any other time) D-28361 Bremen | fax : +49 421 539 4529 (any time) Germany