From: Rick M. <obj...@gm...> - 2014-06-01 19:22:32
|
On Sun, Jun 1, 2014 at 2:50 PM, J. Leslie Turriff <jlt...@ce... > wrote: > From time to time I fiddle around with the oo part of ooRexx, but > to get > anything useful done I always find myself falling back to function-based > programming, which just works. > For example, I'm trying to figure out how to add methods to > read/write data > between an external file and a directory collection, and I'm really > struggling. I modified the example from the V4.2 LangRef, pp. 288, 289 to > store user-defined objects instead of strings in a directory collection. I > finally got that to work (after puzzling over the arcane requirements of > the > setMethod method), but getting the data from an external file into the > directory and putting it back just won't work, and I can't see why. > I created a subclass of .directory to which I added class methods > load and > unload. ooRexx insists on treating them like instance method calls and > triggers the UNKNOWN method instead of invoking them as class methods. > There > are no detailed examples of such usage in the doc as far as I can see; > what I > do find there sheds no light on what I'm doing wrong. > Here's my main program: > > > > > This is the output from PhoneBook3: > | 9 *-* example = .PhoneBook ~ new ~~ > setMethod('UNKNOWN', .methods['UNKNOWN']) > | >>> "a PHONEBOOK" > | 10 *-* say example ~ type > | >>> "a Phone Number directory" > | a Phone Number directory > | 11 *-* example ~ load('Phone.book') > | >>> "No listing was found for LOAD." > | 13 *-* do e over example -- Iterate over the > collection > | >>> "a PHONEBOOK" > | 17 *-* say -- Index lookup is case > sensitive... > | >>> "" > | > | 18 *-* say example ~ entry('Mike') -- The ENTRY method > uppercases before lookup > | >>> "No listing was found for MIKE." > | No listing was found for MIKE. > | 19 *-* say example['ANN'] -- Exact match > | >>> "No listing was found for ANN." > | No listing was found for ANN. > | 20 *-* say example ~ ann -- Message converts to > uppercase before lookup > | >>> "No listing was found for ANN." > | No listing was found for ANN. > | 21 *-* say example['ann'] -- Exact match with > lowercase > index > | >>> "No listing was found for ann." > | No listing was found for ann. > | 22 *-* say -- Use the UNKNOWN method > too > look these up: > | >>> "" > | > | 23 *-* say example['M'] > | >>> "No listing was found for M." > | No listing was found for M. > | 24 *-* say example['J'] > | >>> "No listing was found for J." > | No listing was found for J. > | 25 *-* say example['Z'] > | >>> "No listing was found for Z." > | No listing was found for Z. > | 27 *-* example ~ unload('Phone.book') > | >>> "No listing was found for UNLOAD." > | 28 *-* exit > > So, what am I missing? I suspect it's something that's so > fundamental that > it's not clearly stated in the doc. > I might be able to help you if you gave some indication of why you find the output here to be surprising? I see a number of things here that I find puzzling. Among them: 1) why are you using setmethod to add an unknown method? Your comment asserts that directories don't have an unknown method, but by default, directories DO have an unknown method. If you wish to override the UNKNOWN method, just make it part of your class definition. There is no need to add this via setmethod. 2) The comment before the lookups indicates you are expecting the UNKNOWN method to be used for the lookup, but the UNKNOWN method is not invoked with that sort of lookup, only with the form where you are using a ~name lookup. 3) Your load and unload methods should NOT be class methods, they need to be instance methods. A class method can only be invoked by sending a method to the class object (e.g. by coding .PhoneBook~load). But since these methods are invoking methods on the instance of the phone book (like PUT), they will only work as instance methods. Rick > > Leslie > -- > "Disobedience is the true foundation of liberty. The obedient must be > slaves." --Henry David Thoreau > > > ------------------------------------------------------------------------------ > Time is money. Stop wasting it! Get your web API in 5 minutes. > www.restlet.com/download > http://p.sf.net/sfu/restlet > _______________________________________________ > Oorexx-users mailing list > Oor...@li... > https://lists.sourceforge.net/lists/listinfo/oorexx-users > |