"do" blocks and method args problem
"dennisf486" <[email protected]> Fri, 13 Apr 2012 04:07:05 -0000
| Newsgroups | gmane.comp.lang.io |
|---|---|
| Message-ID | <[email protected]> |
I hit a snag in my script refactoring. I'm trying to wrap all my proto creations inside methods so that I can pass dependencies as arguments, but I can't access the method arguments from inside a do block. This works (because the appendProto is outside the do block): // in file IdMap.io method(namespace_std, self IdMap := Object clone appendProto(namespace_std) do( /* other stuff */ ... ) ) This does not work (but I wish it did): // in file IdMap.io method(namespace_std, self IdMap := Object clone do( appendProto(namespace_std) /* other stuff */ ) The problem is that the method argument "namespace_std" is not in scope inside the do-block. In the case of one appendProto I can easily move the declaration to the outside, but this makes the declaration long when there are a lot of namespace-objects. And, I might want the argument for some other purpose than append proto. I know that I could do something like: self IdMap := Object clone IdMap namespace_std := namespace std IdMap do( ... ) But I'm trying to avoid the repetition of the word "IdMap". I think one possible solution would be to make the method context a proto of the object: method(namespace_std, self IdMap := Object clone appendProto(thisLocalContext) do( appendProto(namespace_std) ) Is this the best / only way? One side effect of this trick is that all of the method invocation's locals / arguments are now permanently uncollectible, even if the args aren't all used, because thisLocalContext has a reference to them and the object has a reference to thisLocalContext. I suppose I could do something weird like appendProto(thisLocalContext) at the top of the file and removeProto(thisLocalContext) at the bottom of the file. If I do this it would make sense to make a method to do that automatically; I would call it "doInLocalContext". Why doesn't Io have a doInLocalContext method already?