comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <sb463ba@d2-hrz.uni-duisburg.de>
Subject: Re: automatic tag conversion?
Date: Sat, 24 May 2003 17:39:54 +0000 (UTC)
Date: 2003-05-24T17:39:54+00:00	[thread overview]
Message-ID: <baoapa$pm7$1@a1-hrz.uni-duisburg.de> (raw)
In-Reply-To: bamcht$fs8$1@news.cs.tu-berlin.de

Stephan Heinemann <zombie@cs.tu-berlin.de> wrote:
: Stephan Heinemann <zombie@cs.tu-berlin.de> wrote:
: 
:> t: Tag;
:> subvar: Subtype;
:> ...
:> t := supervar'Tag;
:> subvar := t'convert(supervar); -- convert(t, supervar)
:> polymorphic(subvar);
: 
: Of course I don't know what the concrete Subtype is and I don't wanna
: know. Therefore the code snippet is wrong. I meant:

If I understand your wording correctly (in Ada, a subtype is
different from a derived type (subtypes add value constraints,
they do not extend)), it might be simpler.
I hope i don't tell matters of course, as I might have missed the
intent of your question.

Consider this small hierarchy.

package R is

   -- a tagged type

   type T is
     tagged record
        null;
     end record;

   function About(it: T) return String;



   type Switch_State is (on, off);
   -- an ad hoc Boolean type, used in the derived type TT

   -- a derived type, extended version of T (what you have called
   -- a subtype, I believe)

   type TT is new T
     with record
        toggle: Switch_State;
     end record;

   function About(it: TT) return String;

end R;

Then, for a variable to be able to represent objects of any type
in this hierarchy, it must either be class-wide, or it
must be an access type (pointer, or reference).

I have tried to make this distintion in the following
snippet:


with R; use R;

procedure U is

   VV: TT;  -- an instance of extended type TT


begin

objects:  -- as opposed to object pointers

    declare
       Current: T := T(VV);
       -- upward conversion, some VV-components gone!
    begin
       -- therefore, the next line doesn't compile, because
       -- it would mean a downward conversion (with missing
       -- TT-components):
       VV := TT(Current);


       -- however, these do compile, because there is a match for every
       -- component needed in a TT instance.

       VV := (Current with toggle => on);
       VV := TT'(Current with toggle => on);
    end objects;


object_pointers:

    declare
       type Poly is access T'class;
       -- Poly values may refer to T-objects or to TT-objects

       a_tt: Poly := new TT;
       a_t: Poly := new T;
    begin
       --  attempts to copy assumed TT values into vv
       vv := TT(a_tt.all);
       vv := TT(a_t.all);  -- tag check will fail at run time
    end object_pointers;

class_wide:    -- (this is probably a very incomplete overview)

    declare
       any1: T'class := TT'(toggle => off);
       any2: T'Class := T'(null record)
    begin
       any1 := any2;  -- run time exception (objects copied, not pointers)
       VV := TT(any1);
    end class_wide;

end U;


HTH,
georg



  reply	other threads:[~2003-05-24 17:39 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-23 23:04 automatic tag conversion? Stephan Heinemann
2003-05-23 23:57 ` Stephan Heinemann
2003-05-24 17:39   ` Georg Bauhaus [this message]
2003-05-25 20:59     ` Stephan Heinemann
2003-05-26  8:29       ` Dmitry A. Kazakov
2003-05-26 18:16         ` Georg Bauhaus
2003-05-26 18:27           ` Georg Bauhaus
2003-05-26 20:49             ` Stephan Heinemann
replies disabled

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