comp.lang.ada
 help / color / mirror / Atom feed
* Ada 83:  Type defaults
@ 1996-09-11  0:00 hehmeyerj
  1996-09-11  0:00 ` Thomas Kendelbacher
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: hehmeyerj @ 1996-09-11  0:00 UTC (permalink / raw)



This may be a simple problem but...

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;)

TIA






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

* Re: Ada 83:  Type defaults
  1996-09-11  0:00 Ada 83: Type defaults hehmeyerj
  1996-09-11  0:00 ` Thomas Kendelbacher
@ 1996-09-11  0:00 ` John G. Volan
  1996-09-12  0:00 ` Ken Garlington
  2 siblings, 0 replies; 4+ messages in thread
From: John G. Volan @ 1996-09-11  0:00 UTC (permalink / raw)
  Cc: John_Volan


hehmeyerj wrote:
> 
> This may be a simple problem but...
> 
> 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;)
> 
> TIA

You can't specify a default value for a scalar type (e.g. enumeration
type) but you can for a record type (whether public or private):

  package P is -- whatever you originally had X in
    type X is private;
    function A return X; -- returns X'(Value => A_Value)
    function B return X; -- returns X'(Value => B_Value)
    function C return X; -- returns X'(Value => C_Value)
    function D return X; -- returns X'(Value => D_Value)
    -- Don't know how much enumeration semantics you need, but you may
    -- need to declare overloadings for "<", ">", "<=", ">=", and 
    -- something that can take the place of 'Succ, 'Pred, etc. If you
really
    -- need to be able to iterate over X in a for loop, you're out of
    -- luck, sorry
  private
    type X_Value is (A_Value, B_Value, C_Value, D_Value);
    type X is record
      Value : X_Value := A_Value; -- default
    end record;
  end P;

  -- package body left as an exercise for the reader :-) ...

------------------------------------------------------------------------
Internet.Usenet.Put_Signature 
  (Name => "John G. Volan",  Home_Email => "John_Volan@syspac.com",
   Employer => "S.A.I.C.",   Work_Email => "John_Volan@dayton.saic.com",
   Slogan => "Ada95: The World's *FIRST* International-Standard OOPL",
   Disclaimer => "My employer never defined these opinions, so using " &
     "them is totally erroneous...or is that a bounded error now? :-)");
------------------------------------------------------------------------




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

* Re: Ada 83:  Type defaults
  1996-09-11  0:00 Ada 83: Type defaults hehmeyerj
@ 1996-09-11  0:00 ` Thomas Kendelbacher
  1996-09-11  0:00 ` John G. Volan
  1996-09-12  0:00 ` Ken Garlington
  2 siblings, 0 replies; 4+ messages in thread
From: Thomas Kendelbacher @ 1996-09-11  0:00 UTC (permalink / raw)



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






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

* Re: Ada 83:  Type defaults
  1996-09-11  0:00 Ada 83: Type defaults hehmeyerj
  1996-09-11  0:00 ` Thomas Kendelbacher
  1996-09-11  0:00 ` John G. Volan
@ 1996-09-12  0:00 ` Ken Garlington
  2 siblings, 0 replies; 4+ messages in thread
From: Ken Garlington @ 1996-09-12  0:00 UTC (permalink / raw)



hehmeyerj wrote:
> 
> This may be a simple problem but...
> 
> 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;)
> 
> TIA

There's the "wrap a record around it" solution (see other posts).

There's the "Make X an abstract data type" solution, which doesn't 
really make the typing easier, but it can be used to _ensure_ that every 
variable of type X is given the same default value.

Also, there's the solution that begins:

1. Upgrade to Ada 95... :)

-- 
LMTAS - "Our Brand Means Quality"




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

end of thread, other threads:[~1996-09-12  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-09-11  0:00 Ada 83: Type defaults hehmeyerj
1996-09-11  0:00 ` Thomas Kendelbacher
1996-09-11  0:00 ` John G. Volan
1996-09-12  0:00 ` Ken Garlington

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