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,33ed4ca582c945fc X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!postnews.google.com!u6g2000prc.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Limited returns Date: Mon, 23 Jun 2008 08:49:01 -0700 (PDT) Organization: http://groups.google.com Message-ID: <2f2354b1-8fca-4675-bee5-fb45b5a8f644@u6g2000prc.googlegroups.com> References: <304d62f5-53eb-4ec2-bcd5-a84ac8f9fe60@m44g2000hsc.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1214236142 8216 127.0.0.1 (23 Jun 2008 15:49:02 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 23 Jun 2008 15:49:02 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: u6g2000prc.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:821 Date: 2008-06-23T08:49:01-07:00 List-Id: On Jun 23, 8:04 am, fedya_fedyak...@inbox.ru wrote: > Hehe, and let's consider this: > > with Ada.Finalization; > procedure Test_Limited_Stuff is > > type E is ( Make_S, Make_S1 ); > > type I is interface; > > type T is new Ada.Finalization.Controlled with record > A : Integer; > end record; > > type S is new T and I with record > --Self : not null access S'Class := S'Unchecked_Access; > B: Integer; > end record; > > type S1 is new T and I with record > --Self : not null access S1'Class := S1'Unchecked_Access; > N: String(1..256); > end record; > > function Factory( w: E ) return I'Class is > begin > if w = Make_S then > return S'(Ada.Finalization.Controlled with A => 0, B => > 1); > else > return S1'(Ada.Finalization.Controlled with A => 0, N => > (others => 10) ); > end if; > end Factory; > > Value : I'Class := Factory(Make_S); > begin > Value := Factory(Make_S1); > end Test_Limited_Stuff; > > Crashed pretty badly with CONSTRAINT_ERROR, whining about tag check > error. Seems its completely buggy and unusable. You may have figured this out already, since you took back this comment. But in case you haven't... when you declare an object of type T'Class (for any tagged type T), you need to initialize it, and then whatever the specific type you initialize it to (in this case, S), the object must have that type for its whole life. You can't assign it to a new value with a different tag. You can only assign it to other values of type S. That's why you got the Tag_Error. As to whether this can be detected at compile time (as you asked in your next post): not likely, since Factory is a function whose return type is declared as I'Class; and it's too much to ask a compiler to go through the function and figure out what *specific* type it would return. Basically it would have to simulate the function's execution in order to do this. A compiler that tries to in-line the call to Factory might be able to figure this out, since the "if" condition in Factory will be known and thus the whole TRUE branch would be eliminated. But that's quite advanced. -- Adam