bseymour@houligan.UUCP (Burch Seymour) (07/07/86)
>>>>> Lion eater food <<<<<
Could someone help me determine if the error in the following Ada code
is in the code or a compiler bug. I have reason to do the following,
package A - Declares an enumeration type
package B - Creates a subtype that is eqivalent to the original
package C - Assigns values using the enumeration values of A into a
variable of the subtype declared in B.
I won't bore you with why I must do it this way as opposed to just
using the original type in package C, but I must. My compiler
generates an undeclared identifier error in the front end, and I can't
tell whether the LRM scoping rules say this is valid or not. Anyone have
any ideas? Any help will be appreciated.
------------------------------------------------------------------------
package A is
type my_type is (st_1,st_2,st_3);
end A;
package body A is
x : my_type;
begin
x := st_1; -- no errors here
end A;
------------------------------------------------------------------------
with A;
package B is
subtype my_type is a.my_type;
end B;
Package body B is
y : my_type;
begin
y := A.st_2; -- no errors here
end B;
------------------------------------------------------------------------
with B;
package C is
I : B.my_type; -- type is OK but appears to fail to bring along
-- enumeration values (see below). The question is...
-- should it?
end C;
Package body C is
begin
I := B.st_3; -- undeclared identifier error on this assign
-- sites section 8.3 of LRM
end c;
------------------------------------------------------------------------
--
-------------------------------------------------------------------------
"A nation that beats its swords into plowshares generally ends up
doing the plowing for one that has kept it swords." Anon
Burch Seymour -Gould C.S.D. at ....mcnc!rti-sel!gould!bseymour
-------------------------------------------------------------------------
esmith@gypsy.UUCP (07/09/86)
Your compiler does the right thing. A subtype declaration does not derive any operations (read "enumeration literals"). If you change the assignment in package B to y := st_2 or y := B.st_2 you will see that the literals aren't locally defined in B, therefore they can't be made available anywhere else through the use of a "with B". Note that if you must do this, and a derived type won't do what you want, you can use renaming declarations: package fruit_mgr is type fruit is (apple, banana, cherry); end fruit_mgt; with fruit_mgr; package pseudo_fruit_mgr is subtype fruit is fruit_mgr.fruit; function apple return fruit renames fruit_mgr.fruit; function banana return fruit renames fruit_mgr.banana; function cherry return fruit renames fruit_mgr.cherry; end pseude_fruit_mgr; You can, of course, just declare constants of type pseudo_fruit_mgr.fruit instead: apple: constant fruit := fruit_mgr.apple; but this method does not preserve the semantics of enumeration literals, particularly with reference to overloading.
joe@petsd.UUCP (Joe Orost) (07/09/86)
In article <91@houligan.UUCP> bseymour@houligan.UUCP (Burch Seymour) writes: >package A is >type my_type is (st_1,st_2,st_3); >end A; > >package body A is >x : my_type; >begin >x := st_1; -- no errors here >end A; >------------------------------------------------------------------------ >with A; >package B is --Solution #1 : replace next line with "TYPE my_type IS NEW a.my_type" >subtype my_type is a.my_type; --Solution #2 : add the following lines: FUNCTION st_1 RETURN my_type RENAMES a.st_1; FUNCTION st_2 RETURN my_type RENAMES a.st_2; FUNCTION st_3 RETURN my_type RENAMES a.st_3; >end B; > >Package body B is >y : my_type; >begin --Change the following to "y := st_2;" >y := A.st_2; -- no errors here >end B; >------------------------------------------------------------------------ >with B; >package C is >I : B.my_type; -- type is OK but appears to fail to bring along > -- enumeration values (see below). The question is... > -- should it? >end C; > >Package body C is >begin >I := B.st_3; -- undeclared identifier error on this assign > -- sites section 8.3 of LRM >end c; Explanation: A subtype declaration does not bring with it the operations (enumeration literals) of the parent type. A DERIVED type declaration does [3.4(6)]. So either use a derived type, or rename all the literals. regards, joe -- Full-Name: Joseph M. Orost UUCP: ihnp4!vax135!petsd!joe ARPA: vax135!petsd!joe@BERKELEY Phone: (201) 758-7284 Location: 40 19'49" N / 74 04'37" W US Mail: MS 313; Concurrent Computer Corporation; 106 Apple St Tinton Falls, NJ 07724