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!newscon06.news.prodigy.com!prodigy.net!newsfeed-00.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Dynamically tagged expression required Date: 09 Dec 2005 11:41:34 -0500 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <2852224.m2vuDFxfOX@linux1.krischik.com> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls4.std.com 1134146494 6162 192.74.137.71 (9 Dec 2005 16:41:34 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Fri, 9 Dec 2005 16:41:34 +0000 (UTC) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Xref: g2news1.google.com comp.lang.ada:6798 Date: 2005-12-09T11:41:34-05:00 List-Id: Maciej Sobczak 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