comp.lang.ada
 help / color / mirror / Atom feed
* Pascal's: Variant Record or C's union
@ 2001-04-06 21:48 Frank
  2001-04-07  3:29 ` DuckE
  0 siblings, 1 reply; 3+ messages in thread
From: Frank @ 2001-04-06 21:48 UTC (permalink / raw)


Hi!

How do I create a record in Ada, that behaves as the variant record in
Pascal or similar to the union in C?
It seems to me that the expression "variant" in Ada is used on how you
define?/declare? :-) types based on a "type with many possible
apperances":-).

Something like:

type TYPE_GOODS is
  record
    ITEM_NAME : string(1..6);
    GOODS_TYPE : integer;
    case GOODS_TYPE <--------???
       when LIQUID:        <---------???
                  LITRE : float;
                  VOLUME_PCT : float;
       when TEXTILE:
                  LENGTH : float;
                  WIDTH : float;
    end case;
  end record;


A : TYPE_GOODS;
..
..
A.ITEM_NAME := 'COGNAC';
A.GOODS_TYPE := LIQUID;
A.LITRE := 0.7;
A.VOLUME_PCT := 45;

A.ITEM_NAME := 'RED   ';
A.GOODS_TYPE := TEXTILE;
A.LENGTH := 10.0;
A.WIDTH := 0.6;




Frank





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

* RE: Pascal's: Variant Record or C's union
@ 2001-04-06 21:59 Beard, Frank
  0 siblings, 0 replies; 3+ messages in thread
From: Beard, Frank @ 2001-04-06 21:59 UTC (permalink / raw)
  To: 'comp.lang.ada@ada.eu.org'

It's very similar to Pascal.

type Type_Of_Goods is (LIQUID, TEXTILE);

type TYPE_GOODS (GOODS_TYPE : Type_Of_Goods) is
  record
    ITEM_NAME : string(1..6);
    case GOODS_TYPE is
       when LIQUID =>
                  LITRE : float;
                  VOLUME_PCT : float;
       when TEXTILE =>
                  LENGTH : float;
                  WIDTH : float;
    end case;
  end record;

Frank


-----Original Message-----
From: Frank [mailto:franjoe@frisurf.no]
Sent: Friday, April 06, 2001 5:48 PM
To: comp.lang.ada@ada.eu.org
Subject: Pascal's: Variant Record or C's union


Hi!

How do I create a record in Ada, that behaves as the variant record in
Pascal or similar to the union in C?
It seems to me that the expression "variant" in Ada is used on how you
define?/declare? :-) types based on a "type with many possible
apperances":-).

Something like:

type TYPE_GOODS is
  record
    ITEM_NAME : string(1..6);
    GOODS_TYPE : integer;
    case GOODS_TYPE <--------???
       when LIQUID:        <---------???
                  LITRE : float;
                  VOLUME_PCT : float;
       when TEXTILE:
                  LENGTH : float;
                  WIDTH : float;
    end case;
  end record;


A : TYPE_GOODS;
..
..
A.ITEM_NAME := 'COGNAC';
A.GOODS_TYPE := LIQUID;
A.LITRE := 0.7;
A.VOLUME_PCT := 45;

A.ITEM_NAME := 'RED   ';
A.GOODS_TYPE := TEXTILE;
A.LENGTH := 10.0;
A.WIDTH := 0.6;




Frank


_______________________________________________
comp.lang.ada mailing list
comp.lang.ada@ada.eu.org
http://ada.eu.org/mailman/listinfo/comp.lang.ada




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

* Re: Pascal's: Variant Record or C's union
  2001-04-06 21:48 Pascal's: Variant Record or C's union Frank
@ 2001-04-07  3:29 ` DuckE
  0 siblings, 0 replies; 3+ messages in thread
From: DuckE @ 2001-04-07  3:29 UTC (permalink / raw)


In Ada, check out "Discriminants".  In the reference section 3.8.1 of the
LRM gives a couple of examples.

Your record definition would look something like:

type GOODS_TYPE is ( LIQUID, TEXTILE);

type TYPE_GOODS( GOODS_TYPE_SELECTION : GOODS_TYPE := LIQUID ) is
  record
    ITEM_NAME : string( 1..6 );
    case GOODS_TYPE_SELECTION is
       when LIQUID =>
           LITRE : float;
           VOLUME_PCT : float;
       when TEXTILE =>
           LENGTH : float;
           WIDTH : float;
    end case;
  end record;

But you'll find that these records are less promiscuous than their
counterpart in 'C' or Pascal.

In Ada the following 3 type declarations have different meanings.

  A : TYPE_GOODS;
  B : TYPE_GOODS( LIQUID );
  C : TYPE_GOODS( TEXTILE );

In the record definition for "TYPE_GOODS" a "default discriminant" is
provided (that's the := LIQUID in the type declaration).  Since their is a
default discriminant, the declaration of "A" will by default use the LIQUID
variant.  Also, since the default is used in the declaration of "A", you may
assign a new value to "A" from any record of type "TYPE_GOODS", whether it
has LIQUID or TEXTILE variant.

The declaration of "B" specifies the LIQUID variant is used.  This variable
can only contain values for the LIQUID variant form.

The declaration of "C" specifies the TEXTILE variant is used.  This variable
can only contain values of the TEXTILE form.

In Ada you may never assign a value to "GOODS_TYPE_SELECTION" except as part
of a complete record assignment, and then only when the destination record
is either of the same variant as the source of the assignment, or the
destination is declared using the default descriminant.

So for your assignments from your example:

  A := ( GOODS_TYPE_SELECTION => LIQUID,
            ITEM_NAME => 'COGNAC',
            LITRE => 0.7,
            VOLUME_PCT := 45 );

  A := (GOODS_TYPE_SELECTION => TEXTILE,
            ITEM_NAME => 'RED   ',
            LENGTH => 10.0,
            WIDTH => 0.6 );

This may sound a little bit involved, but it eliminates the case that occurs
in both Pascal and 'C' where you can assign a value to a component of a
record viewing the record as one variant type and retrieve the value viewing
the record as a different variant.

BTW: If that is what you're interested in, check out "Unchecked_Convesion"
instead.

I hope this helps,
And I hope I got it right (that's my understanding anyway),
I didnt' compile my examples, so please have mercy,

SteveD

"Frank" <franjoe@frisurf.no> wrote in message
news:UQqz6.6485$NR.552875@news3.oke.nextra.no...
> Hi!
>
> How do I create a record in Ada, that behaves as the variant record in
> Pascal or similar to the union in C?
> It seems to me that the expression "variant" in Ada is used on how you
> define?/declare? :-) types based on a "type with many possible
> apperances":-).
>
> Something like:
>
> type TYPE_GOODS is
>   record
>     ITEM_NAME : string(1..6);
>     GOODS_TYPE : integer;
>     case GOODS_TYPE <--------???
>        when LIQUID:        <---------???
>                   LITRE : float;
>                   VOLUME_PCT : float;
>        when TEXTILE:
>                   LENGTH : float;
>                   WIDTH : float;
>     end case;
>   end record;
>
>
> A : TYPE_GOODS;
> ..
> ..
> A.ITEM_NAME := 'COGNAC';
> A.GOODS_TYPE := LIQUID;
> A.LITRE := 0.7;
> A.VOLUME_PCT := 45;
>
> A.ITEM_NAME := 'RED   ';
> A.GOODS_TYPE := TEXTILE;
> A.LENGTH := 10.0;
> A.WIDTH := 0.6;
>
>
>
>
> Frank
>
>





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

end of thread, other threads:[~2001-04-07  3:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-04-06 21:48 Pascal's: Variant Record or C's union Frank
2001-04-07  3:29 ` DuckE
  -- strict thread matches above, loose matches on Subject: below --
2001-04-06 21:59 Beard, Frank

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