comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Limited returns
Date: Mon, 23 Jun 2008 08:49:01 -0700 (PDT)
Date: 2008-06-23T08:49:01-07:00	[thread overview]
Message-ID: <2f2354b1-8fca-4675-bee5-fb45b5a8f644@u6g2000prc.googlegroups.com> (raw)
In-Reply-To: 304d62f5-53eb-4ec2-bcd5-a84ac8f9fe60@m44g2000hsc.googlegroups.com

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






  parent reply	other threads:[~2008-06-23 15:49 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-23 14:21 Limited returns Dmitry A. Kazakov
2008-06-23 15:04 ` fedya_fedyakoff
2008-06-23 15:20   ` fedya_fedyakoff
2008-06-23 16:53     ` Dmitry A. Kazakov
2008-06-24 10:56       ` fedya_fedyakoff
2008-06-24 13:51         ` Dmitry A. Kazakov
2008-06-24 15:01           ` fedya_fedyakoff
2008-06-24 16:31             ` Dmitry A. Kazakov
2008-06-24 16:42               ` Dmitry A. Kazakov
2008-06-23 15:49   ` Adam Beneschan [this message]
2008-06-24 10:13     ` fedya_fedyakoff
2008-06-23 15:15 ` Adam Beneschan
2008-06-23 17:03   ` Dmitry A. Kazakov
2008-06-23 18:15     ` Adam Beneschan
2008-06-23 19:44       ` Dmitry A. Kazakov
2008-06-26 12:35 ` Egil Høvik
2008-06-26 14:12   ` Dmitry A. Kazakov
replies disabled

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