Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!ncar!gatech!rutgers!bellcore!faline!thumper!ulysses!andante!alice!dbl From: dbl@alice.UUCP (David Levine, for Kim Bruce) Newsgroups: comp.lang.eiffel Subject: constrained generic problem Keywords: constrained generics, inheritance Message-ID: <9091@alice.UUCP> Date: 23 Mar 89 16:00:37 GMT Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 82 I have been having problems with constrained generics. Can anyone give me some advice. I have a class called SIMPLE_NODE which handles requests of the form: get_info and print_node (all it does is package up a variable name and simple operations on it). I also have a subclass of SIMPLE_NODE called COMPLEX_NODE (what else!) which adds an attribute phone_number and modifies get_info and print_node to include getting and printing phone_number as well as name. NAME_TREE[T] inherits from BIN_SRCHTREE[T], redefining node_action to call print_node. I also have a class tree_menu[T -> SIMPLE_NODE] (see below) which is constrained generic, and which also has a local variable new_node : T. --------------------------------------- class tree_menu[T -> SIMPLE_NODE] inherit STD_FILES feature ...; my_tree : NAME_TREE[T] ; create is local new_node : T do from done:=false until done loop if .. then...; elsif cmd='a' then new_node.create; next_line ; new_node.get_info; if my_tree.void then my_tree.create(new_node) else my_tree.insert(new_node) end; elsif ...; else ...; end end; end; getcommand : CHARACTER is do ... end ; end --tree_menu ---------------------------------- The problem is that no matter how I instantiate the T, I always get the operations in SIMPLE_NODE on new_node. E.g. the call new_node.getinfo above always calls the operation defined in SIMPLE_NODE even if I define a variable, complex_menu : TREE_MENU[COMPLEX_NODE], and then write: complex_menu.Create Interestingly if I remove the generics from TREE_MENU and just replace T everywhere by COMPLEX_NODE, it works fine. So the problem clearly has to do with the use of generics. Can anyone help me on this? Is this a compiler error that has arisen since constrained generics are so new? I have found a work-around based on eliminating the use of constrained generics (basically write TREE_MENU using SIMPLE_NODE, including a dummy anchor, and then declaring my tree to be of class NAME_TREE[like dummy_anchor], and then defining a new subclass of tree_menu that redefines the class of dummy_anchor to be COMPLEX_NODE. However, I consider this to be a much less elegant solution to the problem. The notion of constrained genericity (really the "bounded quantification" of Cardelli and Wegner) is a much better way of retaining control of the types of variables than the rather ad hoc techniques of using anchors. Kim Bruce Williams College kim@cs.williams.edu P.S. Is anyone else having the problem of getting '5's all over the place when trying to use the interactive debugger?