From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,154942e4f1d1b8e9 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!130.59.10.21.MISMATCH!kanaga.switch.ch!switch.ch!newsfeed.inode.at!news.hispeed.ch!linux2.krischik.com!news From: Martin Krischik Newsgroups: comp.lang.ada Subject: Re: Dynamically tagged expression required Date: Fri, 09 Dec 2005 21:16:04 +0100 Organization: Cablecom Newsserver Message-ID: <4718553.l2jsoQ7pK7@linux1.krischik.com> References: <2852224.m2vuDFxfOX@linux1.krischik.com> NNTP-Posting-Host: 84-73-0-2.dclient.hispeed.ch Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: news.hispeed.ch 1134198932 16949 84.73.0.2 (10 Dec 2005 07:15:32 GMT) X-Complaints-To: news@hispeed.ch NNTP-Posting-Date: Sat, 10 Dec 2005 07:15:32 +0000 (UTC) User-Agent: KNode/0.9.2 Xref: g2news1.google.com comp.lang.ada:6806 Date: 2005-12-09T21:16:04+01:00 List-Id: 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