Path: utzoo!utgpu!utstat!jarvis.csri.toronto.edu!mailrus!ames!ucsd!orion.cf.uci.edu!oberon!sm.unisys.com!randvax!florman From: florman@randvax.UUCP (Bruce Florman) Newsgroups: comp.lang.eiffel Subject: Re: Posting on behalf of CNET users Message-ID: <1905@randvax.UUCP> Date: 10 Mar 89 23:46:07 GMT References: <112@eiffel.UUCP> Reply-To: florman@rand-unix.UUCP (Bruce Florman) Organization: Rand Corp., Santa Monica, Ca. Lines: 124 In article <112@eiffel.UUCP> Jean-Michel Cornily writes: >We are just beginning to use Eiffel and are confronted with some >problems. One of them is the following : >We would like to apply routines on sets of objects, but there seems to >have problems with types. Here is an example (let us suppose that our >set is implemented as a list, and the routine that we want to distribute >is like a "procedure") : > >class A >feature > > set_do_all (action : X) is > do > from set.start > until set.offright > loop > set.value.action; > set.forth; > end; > end ; -- set_do_all > >end; -- class A > >Our problem here is to determine what is X (the type of action). Is there >a type "routine" ? If not, we cannot define such a routine. One way of doing this sort of thing is to create a class of objects which know how to be applied to other objects. This class would look something like the following (my apologies to anyone offended by my use of all caps for reserved words, but this terminal doesn't have a boldface key). CLASS INSTRUCTION [T] EXPORT apply_to FEATURE apply_to (target: T) IS REQUIRE NOT target.Void; DEFERRED END; END -- CLASS INSTRUCTION Using this class, we can now implement a set of objects that knows how to apply an instruction to all of its members. It would look something like this: CLASS SET [T] EXPORT apply_to_all INHERIT LINKED_LIST [T]; FEATURE apply_to_all (action: INSTRUCTION [T]) IS DO FROM start UNTIL offright LOOP action.apply_to(value); forth; END; END; END -- CLASS SET A drawback of this method is that you now have to create a new class for every instruction that you want to apply somewhere. However, such classes should be pretty small. For example, the following class knows how to call a string's left_adjust routine. CLASS LEFT_ADJUST_INSTRUCTION INHERIT INSTRUCTION [STRING] REDEFINE execute; FEATURE apply_to (target: STRING) IS REQUIRE NOT target.Void; DO target.left_adjust; END; END -- CLASS LEFT_ADJUST_INSTRUCTION To use this class, you would have to declare the following entities somewhere in your code. my_string_set: SET [STRING]; left_adjust: LEFT_ADJUST_INSTRUCTION; Now when the two Eiffel instructions below are executed, every string in my_string_set will become left adjusted. left_adjust.Create; my_string_set.apply_to_all(left_adjust); This is obviously quite a bit more verbose than it would be in languages which can pass procedural parameters (eg. Pascal), but it does accomplish what you wanted. Hope this helps, -Bruce Florman florman@rand-unix.ARPA