comp.lang.ada
 help / color / mirror / Atom feed
From: mfeldman@seas.gwu.edu (Michael Feldman)
Subject: Tagged type feature, or GNAT bug?
Date: 27 Sep 1994 12:36:30 -0400
Date: 1994-09-27T12:36:30-04:00	[thread overview]
Message-ID: <369hme$e9j@felix.seas.gwu.edu> (raw)

-- I'm exploring tagged types as an alternative to variant records.
-- Seemingly, one should be permitted to use a classwide type as
-- one would an unconstrained variant record. But see below:

PROCEDURE TestTags IS

-- First a classical Ada 83 variant record
-- Note that the record is unconstrained

  TYPE Kinds IS (A, B, C);

  TYPE VariantRec (Discrim: Kinds := A) IS RECORD
    FixedPart: String (1..4);
    CASE Discrim IS
      WHEN A =>
        F1: Integer;
      WHEN B =>
        F2: Float;
      WHEN C =>
        F3: Boolean;
    END CASE;
  END RECORD;

-- Now a tagged type equivalent

  TYPE TaggedRec IS TAGGED RECORD
    FixedPart: String(1..4);
  END RECORD;

  TYPE A_TaggedRec IS NEW TaggedRec WITH RECORD
    F1: Integer;
  END RECORD;

  TYPE B_TaggedRec IS NEW TaggedRec WITH RECORD
    F2: Float;  
  END RECORD;

  TYPE C_TaggedRec IS NEW TaggedRec WITH RECORD
    F3: Boolean;
  END RECORD;

-- OK, some arrays now.

-- CASE 1: array of unconstrained variant records. OK in Ada 83.

  TYPE List IS ARRAY (1..4) OF VariantRec; -- unconstrained variant rec
  Stuff: List;

-- CASE 2: Now an array intended to hold any of the four types
-- (by direct analogy with Case 1)

-- GNAT rejects this - says the array elements must be constrained

  TYPE TaggedList IS ARRAY (1..4) OF TaggedRec'Class;
  TaggedStuff: TaggedList;

-- CASE 3: Array of_pointers_ to tagged records
-- This will compile OK, but seems a gratuitously required use
-- of pointers - very un-Ada-like.

  TYPE TaggedRecPtr IS ACCESS TaggedRec'Class; 
  TYPE TaggedListPtr IS ARRAY (1..4) OF TaggedRecPtr;
  TaggedStuffPtr: TaggedList;

-- So what is the solution? Perhaps I missed something in 9XRM 5.0,
-- but I found nothing to prohibit what I tried to do in Case 2.

-- The Ada 9X textbook examples make much of the fact that one
-- _can_ use pointers more flexibly in 9X, because e.g. the
-- designated objects need not be dynamic. They also show pointers
-- as a way that one _can_ do dispatching of operations on tagged-type
-- derivatives. It is not clear where (or why) this is the _only_
-- allowed way.

-- So did I bump into an Ada 9X feature, or just a GNAT bug?

BEGIN

  NULL;

END TestTags;

-- Mike Feldman



             reply	other threads:[~1994-09-27 16:36 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-09-27 16:36 Michael Feldman [this message]
1994-09-27 23:22 ` Tagged type feature, or GNAT bug? Bob Duff
1994-09-28 11:14 ` Robert Dewar
replies disabled

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