From: Martin Krischik <krischik@users.sourceforge.net>
Subject: Re: Dynamically tagged expression required
Date: Fri, 09 Dec 2005 21:16:04 +0100
Date: 2005-12-09T21:16:04+01:00 [thread overview]
Message-ID: <4718553.l2jsoQ7pK7@linux1.krischik.com> (raw)
In-Reply-To: dnbhjf$1fa$1@sunnews.cern.ch
Maciej Sobczak wrote:
> 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);
You know this procedure won't dispatch?
> T : Triangle;
> R : Rectangle;
>
> and later:
>
> Draw(T); -- S initialized with T
> Draw(R); -- S initialized with R
>
> Right?
Yes. But it only look like the parameter is initialized. Internally the
compiler likely to use a pointer to pass the paramer to speed up things.
Remember "in" parameter are also constant.
And for the dispatching calls the compiler allways uses a pointer.
>>>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?
>
> 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.
Once the variable is initialized it becomes constained - you can only
assigned object of the same constain to them.
Simpler example:
declare
Text : String := "Hello"; - (A)
begin
Text := "World" - (B)
Text := "World!" - (C)
end
With (A) String becomes constrained to (1 .. 5). The assignment (B) is ok as
well. However (C) will fail - with the exclamation mark the text is 6
characters and the assignment will fail.
> 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?
To tell the truth: Up until now I have only initialized 'Class variables but
never reassigned them with a new value. Even more I just rename them:
http://en.wikibooks.org/wiki/Ada_Programming/Subtypes#Rename_view
That saves copying the data.
'Class is also usefull as element type for container types which can store
indefinite elements.
Martin
--
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com
next prev parent reply other threads:[~2005-12-09 20:16 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
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 [this message]
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