Re: Limited returns



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



.



Relevant Pages

  • Re: Philosophical question on collection reference types
    ... Piotr Kobzda wrote: ... factory method like that: ... Here is a mistake in my treatment of second type parameter, not a generics implementation nor a compiler fault. ... it can be done correctly using factory object implementing the following-like interface: ...
    (comp.lang.java.programmer)
  • Re: generic parameter mit new erzeugen
    ... > private F Factory; ... > Dummerweise sagt mir der Compiler "Cannot instantiate the type F". ... LevelBuilder(Class_f) throws InstantiationException, ... Next by Date: ...
    (de.comp.lang.java)
  • Re: Sutters Pimples: Good, Bad, or Ugly?
    ... > to start examining the headers in some of the programs I have the source ... On small projects the compiler does most of its work parsing all the ... I suppose that people would say that factory is the pattern for this but it ... class and its pimpl. ...
    (comp.lang.cpp)
  • Re: Lösung gesucht: Factorx und falsch instanisiert
    ... wenn es darum geht die Stellen zu finden, an denen an der Factory ... Dir der compiler die maroden Stellen. ... Am Schluss darfst Du nur noch in der ... von Instanzen zentral ablaufen zu lassen. ...
    (de.comp.lang.java)
  • Re: Replicating results
    ... gradient calculation used in a large optimization routine. ... It will make sure that if you do something like declare a variable called "x1" and use a variable called "xl" the compiler will tell you that xl was undeclared. ... Make sure that you initialize all of your variables, ...
    (comp.lang.fortran)