comp.lang.ada
 help / color / mirror / Atom feed
From: Thomas.Kendelbacher@erno.de (Thomas Kendelbacher)
Subject: Re: Ada 83:  Type defaults
Date: 1996/09/11
Date: 1996-09-11T00:00:00+00:00	[thread overview]
Message-ID: <516rqo$e4h@mailsrv2.erno.de> (raw)
In-Reply-To: 516mkh$d3m@ryker.dma.gov


In article <516mkh$d3m@ryker.dma.gov>, hehmeyerj <hehmeyerj@dma.gov> 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






  reply	other threads:[~1996-09-11  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-09-11  0:00 Ada 83: Type defaults hehmeyerj
1996-09-11  0:00 ` Thomas Kendelbacher [this message]
1996-09-11  0:00 ` John G. Volan
1996-09-12  0:00 ` Ken Garlington
replies disabled

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