Re: Limited returns
- From: Adam Beneschan <adam@xxxxxxxxxx>
- Date: Mon, 23 Jun 2008 08:49:01 -0700 (PDT)
On Jun 23, 8:04 am, fedya_fedyak...@xxxxxxxx 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
.
- Follow-Ups:
- Re: Limited returns
- From: fedya_fedyakoff
- Re: Limited returns
- References:
- Limited returns
- From: Dmitry A. Kazakov
- Re: Limited returns
- From: fedya_fedyakoff
- Limited returns
- Prev by Date: Re: Limited returns
- Next by Date: Re: Limited returns
- Previous by thread: Re: Limited returns
- Next by thread: Re: Limited returns
- Index(es):
Relevant Pages
|