Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!mips!spool.mu.edu!munnari.oz.au!goanna!wren!ajk From: ajk@wren.cs.rmit.OZ.AU (Alan Kent) Newsgroups: comp.lang.eiffel Subject: Re: How can I do this in Eiffel? Message-ID: Date: 16 Apr 91 02:11:59 GMT References: <1991Apr15.131833.230@bony1.bony.com> Sender: news@goanna.cs.rmit.oz.au Distribution: comp Lines: 69 richieb@bony1.bony.com (Richard Bielak) writes: >In article ajk@wren.cs.rmit.OZ.AU (Alan Kent) writes: >>I have a tree structure of nodes. These nodes are not all of exactly the >>same class.... >You can do exactly what you need by using the "reverse assigment" in >Eiffel. Reverse assigment is one of few ways, in which you can check >that two objects are type compatible. Here is how this might work: > visit (n : NODE; t : SPECIFIC_TYPE_OF_NODE) is > do > t ?= n; -- reverse assigment > if not t.Void then > -- here t referes to the specific type > -- so apply whatever things > t.specific_routine > end; > end; -- visit >This routine can then be called for every node in your tree. >In the above example, I assume that NODE is a parent of >SPECIFIC_NODE_TYPE. The "?=" will return a non-void value only when >"n" actually references SPECIFIC_NODE_TYPE. First, I assume you mean t is local, not an argument? I thought you were not allowed to assign to arguments. Also, I am not sure how this solves my problem. Say I have 30 different "specific_routine"s. Do I have to then write 30 different visit functions with 30 different walk functions to call the visit functions? Also, what class is visit a method of? In my case I wanted to write some code that could be applied to every node in the tree without chaning the definition of the tree nodes. For example, if I want to count the number of IF nodes in the tree. To me it seems pretty yucky to have to have to change my definition of an IF node (and possibily all the other nodes too) just so somewhere else I can count the number of IF nodes in the tree. Someone asked me to post any mail I got, so here is a suggestion I got in the mail a bit back (I have been off air for a while). > I think you define a new class and override the one or two methods > of interest, and perhaps add or augment a few other methods. Much cleaner > than passing a function pointer around. On the other hand, it could lead > to a lot of useless classes. In the worst case, I guess you could just > write some C code and hook it into Eiffel. -- > -- Ari Halberstadt > ari@eleazar.dartmouth.edu The best I could think up is to try and do something like lists in Eiffel. ie. make the tree walking code keep a current state so you can do code like start at top node in tree while more nodes to be examined do whatever on current node move to next node end Thinking about it a few seconds more this does appear to solve my problem. You could probably write breadth first and depth first tree walking routines if you wanted to. The tree walk code might be a bit tricky, but not too bad. Maybe also provide methods to do things like return parentage array, current depth and so on. Can anyone see anything wrong with this? Thanks Alan Kent ajk@goanna.cs.rmit.oz.au