comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Dynamically tagged expression required
Date: 09 Dec 2005 11:41:34 -0500
Date: 2005-12-09T11:41:34-05:00	[thread overview]
Message-ID: <wccirtygr41.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: dnbhjf$1fa$1@sunnews.cern.ch

Maciej Sobczak <no.spam@no.spam.com> writes:

> Martin Krischik wrote:
> 
> >>    A : Shape;
> >>    B : Shape'Class := A;             -- (1)
> >>    C : Triangle := (SideLen => 7);
> 
> >>begin
> >>
> >>    A := C;                           -- (2)
> >>    B := C;                           -- (3)
> >>
> >>end Hello;
> 
> >>Why is (1) allowed?
> > because it is an initialization.
> 
> OK, so that explains why it's possible to use Shape'Class as a parameter
> in subroutine. Every time the subroutine is called, the parameter is
> *initialized* with the value, and therefore it can be different for each
> call:
> 
> procedure Draw(S : Shape'Class);
> 
> T : Triangle;
> R : Rectangle;
> 
> and later:
> 
> Draw(T);   -- S initialized with T
> Draw(R);   -- S initialized with R
> 
> Right?

Right.

> >>Why is (3) not allowed?
> > because it is an assignment.
> 
> And this is where I want to dig deeper.
> 
> > http://en.wikibooks.org/wiki/Ada_Programming/Object_Orientation
> 
> This page says:
> 
> "The Class type is a real data type. You can declare variables of the
> class-wide type, assign values to them [...]"
> 
> "Assign values to them" is a problem here.
> As the subject of my original post says (and what compiler told me),
> "dynamically tagged expression required".
> 
> What can I provide as a dynamically tagged expression?

You can convert to class-wide type.  But if the Tag is different,
that just turns a compile-time error into a run-time error.

> It looks like some ShapeClassPtr.all is OK there, except that I get the
> CONSTRAINT_ERROR, if the new value has a different tag. This observation
> agrees with what Dmitry said - the tag of the class-wide object cannot
> be changed.
> 
> What other dynamically tagged expression can be provided as a right-hand
> side of the (default) assignment to the object of class-wide type?
> Is this useful in practice?

Not very useful.  It would work in a case where you have some object Obj
of type Shape'Class, and you do something like:

    if Obj'Tag = A'Tag then
        A := Obj;

But that sort of thing doesn't come up very often.

If you want to change the Tag of an object, you can't -- you have to use
access types, and allocate a new object with the new Tag you want.
In my experience, class-wide types nearly always require use
of access types and heap allocation.

The reason Tags can't change is similar to the reason discriminants
can't change.  (Well, they can if the discrim has a default value,
but that's not allowed for tagged types.  We tried to allow it,
but it caused so many semantic anomalies that we gave up.
Tags don't have defaults, either.)

If Tags could change, then the size of objects could change, which would
pretty much require a heap-based implementation.  Ada doesn't like
implicit heap allocation, and so places this burden on the programmer.
The defaulted-discrim case (for untagged types) is different,
because the compiler can calculate the max possible size.

- Bob



  parent reply	other threads:[~2005-12-09 16:41 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-08 13:50 Dynamically tagged expression required Maciej Sobczak
2005-12-08 14:46 ` Georg Bauhaus
2005-12-08 14:52 ` Dmitry A. Kazakov
2005-12-08 19:17 ` Martin Krischik
2005-12-09  9:09   ` Maciej Sobczak
2005-12-09 12:05     ` Jean-Pierre Rosen
2005-12-09 16:41     ` Robert A Duff [this message]
2005-12-09 20:18       ` Martin Krischik
2005-12-11  0:43         ` Matthew Heaney
2005-12-11 11:45           ` Martin Krischik
2005-12-11 14:24             ` Matthew Heaney
2005-12-11 15:18               ` Martin Krischik
2005-12-11 18:07             ` Matthew Heaney
2005-12-12 19:16               ` Martin Krischik
2005-12-13 20:43                 ` Georg Bauhaus
2005-12-13 21:00                   ` Georg Bauhaus
2005-12-09 20:41       ` Randy Brukardt
2005-12-09 20:16     ` Martin Krischik
2005-12-11  0:46       ` Matthew Heaney
2005-12-12  9:44       ` Maciej Sobczak
2005-12-12 19:13         ` Martin Krischik
2005-12-14 10:22           ` Maciej Sobczak
2005-12-15 20:10             ` Martin Krischik
replies disabled

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