comp.lang.ada
 help / color / mirror / Atom feed
* question on variant record
@ 1996-11-02  0:00 Michiel Perdeck
  1996-11-03  0:00 ` Matthew Heaney
  0 siblings, 1 reply; 6+ messages in thread
From: Michiel Perdeck @ 1996-11-02  0:00 UTC (permalink / raw)



This is one of several questions on Ada95. I have split them over a
few messages so that reactions may be given to individual questions.

Can the tag field in a variant record (the field that determines the
actual type of the record) be queried just like a normal field? It
makes sense that it cannot be set afterwards but one should want to
request it's actual value, I suppose.

Regards,
Michiel Perdeck

Michiel Perdeck
CMG AT Finance, Amstelveen, Netherlands
michiel.perdeck@cmg.nl
michiel.perdeck@tip.nl





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

* question on variant record
@ 1996-11-02  0:00 Michiel Perdeck
  1996-11-03  0:00 ` David C. Hoos, Sr.
  0 siblings, 1 reply; 6+ messages in thread
From: Michiel Perdeck @ 1996-11-02  0:00 UTC (permalink / raw)



This is one of several questions on Ada95. I have split them over a
few messages so that reactions may be given to individual questions.

Can the tag field in a variant record (the field that determines the
actual type of the record) be queried just like a normal field? It
makes sense that it cannot be set afterwards but one should want to
request it's actual value, I suppose.

Regards,
Michiel Perdeck

Michiel Perdeck
CMG AT Finance, Amstelveen, Netherlands
michiel.perdeck@cmg.nl
michiel.perdeck@tip.nl





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

* Re: question on variant record
  1996-11-02  0:00 question on variant record Michiel Perdeck
@ 1996-11-03  0:00 ` Matthew Heaney
  1996-11-14  0:00   ` Robert Dewar
  0 siblings, 1 reply; 6+ messages in thread
From: Matthew Heaney @ 1996-11-03  0:00 UTC (permalink / raw)



In article <E090qu.JpG@tip.nl>, michiel.perdeck@cmg.nl wrote:

>Can the tag field in a variant record (the field that determines the
>actual type of the record) be queried just like a normal field? It
>makes sense that it cannot be set afterwards but one should want to
>request it's actual value, I suppose.

The technical term is "discriminant," not "tag field."  And technically, a
"variant record" is a discriminant record only with a variant part (case
statement).  You probably meant "discriminant record" in your subject line.

Yes, you can always query the value of the discriminant, even for
discriminated private types.

For example,

   type Sequence (Size : Positive) is private;
...
   The_Sequence : Sequence (132);

   The_Size : constant Positive := The_Sequence.Size;
...

The thing about discriminant records is whether or not you want objects of
the type to be mutable (able to change the value of the discriminant) or to
be a component of a composite type (an array or another record).

A discriminated type without a default descriminant is "indefinate"; you
have to specify a value of the discriminant when declaring objects of the
type, and the object is then constrained to that discriminant value.  The
type Sequence above is an example.

If the discriminated type has a default discriminant, and you declare the
object without a discriminant value, then the object is mutable, and you
can change its discriminant value.  For example,

   type T_Kind is (T1, T2, T3);
   type T (Kind : T_Kind := T_Kind'First) is record ...;

   O : T;
begin
   O := (T1, ...);
   O := (T2, ...);
   O := (T3, ...);

However, if you declare the object with a descriminant, the object is
constrained to that value, even if the type has a default:

   O : T (T2);
begin
   O := (T3, ...);  -- illegal

One technique is to use the discriminant of the record as an index
constraint of an array component.  This is one way of making ragged arrays
in Ada:

   subtype String_Buffer_Length_Range is Natural range 0 .. 80;
   type String_Buffer (Length : String_Buffer_Length_Range := 0) is
      record
         Text : String (1 .. Length);
      end record;

   type String_Buffer_Array is array (Positive range <>) of String_Buffer;

   function "+" (S : String) return String_Buffer is
   begin
      return (S'Length, S);  -- in Ada 83, make sure you slide S 
   end;

   SB : String_Buffer_Array (1 .. 10);
begin
  SB (1) := +"hello";
  SB (2) := +"goodbye";
  SB (3) := +"Ada Lovelace";
  SB (4) := +"Charles Babbage";

Of course, in all these examples, you can always read the value of the
discriminant.

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
mheaney@ni.net
(818) 985-1271




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

* Re: question on variant record
  1996-11-02  0:00 Michiel Perdeck
@ 1996-11-03  0:00 ` David C. Hoos, Sr.
  0 siblings, 0 replies; 6+ messages in thread
From: David C. Hoos, Sr. @ 1996-11-03  0:00 UTC (permalink / raw)



The answer is yes -- but not "just like a normal field".
Tagged types have a 'Tag attribute, returning a value of type Ada.Tags.Tag
(a private type), and an attribute 'External_Tag which is a string.  The
package Ada.Tags has conversions between the two attributes.  For more
details see the LRM, section 3.9.(5-25), and 13.3 (74-78).
-- 
David C. Hoos, Sr.,
http://www.dbhwww.com
http://www.ada95.com

Michiel Perdeck <michiel.perdeck@tip.nl> wrote in article
<E090tr.Kru@tip.nl>...
> This is one of several questions on Ada95. I have split them over a
> few messages so that reactions may be given to individual questions.
> 
> Can the tag field in a variant record (the field that determines the
> actual type of the record) be queried just like a normal field? It
> makes sense that it cannot be set afterwards but one should want to
> request it's actual value, I suppose.
> 
> Regards,
> Michiel Perdeck
> 
> Michiel Perdeck
> CMG AT Finance, Amstelveen, Netherlands
> michiel.perdeck@cmg.nl
> michiel.perdeck@tip.nl
> 
> 




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

* Re: question on variant record
  1996-11-03  0:00 ` Matthew Heaney
@ 1996-11-14  0:00   ` Robert Dewar
  1996-11-15  0:00     ` Matthew Heaney
  0 siblings, 1 reply; 6+ messages in thread
From: Robert Dewar @ 1996-11-14  0:00 UTC (permalink / raw)



Matthew Heany said

"The technical term is "discriminant," not "tag field."  And technically, a
"variant record" is a discriminant record only with a variant part (case
statement).  You probably meant "discriminant record" in your subject line."

variant record is a perfectly good term, see for example para 35 of the
introduction to the RM:

35   Record, task, and protected types may have special components called
discriminants which parameterize the type.  Variant record structures that
depend on the values of discriminants can be defined within a record type.


variant_part is a perfectly good syntactic term, so the term variant
record is a perfectly reasonable one, it  means a record with a variant
part.

The other term is discriminaTED record, which is a record type with
discriminants. Note that all variant records have discriminants, but
not all records with discriminants have variant parts, so variant record
is a useful term.

P.S. the term discriminant record is not used in the RM.





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

* Re: question on variant record
  1996-11-14  0:00   ` Robert Dewar
@ 1996-11-15  0:00     ` Matthew Heaney
  0 siblings, 0 replies; 6+ messages in thread
From: Matthew Heaney @ 1996-11-15  0:00 UTC (permalink / raw)



In article <dewar.847999613@merv>, dewar@merv.cs.nyu.edu (Robert Dewar) wrote:

>Matthew Heany said
>
>"The technical term is "discriminant," not "tag field."  And technically, a
>"variant record" is a discriminant record only with a variant part (case
>statement).  You probably meant "discriminant record" in your subject line."
>
>variant record is a perfectly good term, see for example para 35 of the
>introduction to the RM:
>
>35   Record, task, and protected types may have special components called
>discriminants which parameterize the type.  Variant record structures that
>depend on the values of discriminants can be defined within a record type.
>
>
>variant_part is a perfectly good syntactic term, so the term variant
>record is a perfectly reasonable one, it  means a record with a variant
>part.
>
>The other term is discriminaTED record, which is a record type with
>discriminants. Note that all variant records have discriminants, but
>not all records with discriminants have variant parts, so variant record
>is a useful term.
>
>P.S. the term discriminant record is not used in the RM.

I stand corrected.  Thank you for clarifying this.

M.

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
mheaney@ni.net
(818) 985-1271




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

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

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-11-02  0:00 question on variant record Michiel Perdeck
1996-11-03  0:00 ` Matthew Heaney
1996-11-14  0:00   ` Robert Dewar
1996-11-15  0:00     ` Matthew Heaney
  -- strict thread matches above, loose matches on Subject: below --
1996-11-02  0:00 Michiel Perdeck
1996-11-03  0:00 ` David C. Hoos, Sr.

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