Re: "do" blocks and method args problem
"dennisf486" <[email protected]> Fri, 13 Apr 2012 04:17:55 -0000
| Newsgroups | gmane.comp.lang.io |
|---|---|
| Message-ID | <[email protected]> |
Haha OK I feel stupid, I just found "lexicalDo" in Io's C source code, it appears it does exactly that. Why doesn't lexicalDo show among the documented Object methods in the Io Reference on the website? And may I humbly suggest adding lexicalDo to the Guide and/or the Tutorial? I think this might be a pain point for new users of Io; I'm not arguing that the default behavior of regular "do" shouldn't be what it is, but I do think that it doesn't necessarily match a new user's intuition, especially if they come from "strongly lexically scoped" languages. Presenting "do" and "lexicalDo" side-by-side would both avert that potential confusion and be a primer for understanding scoping rules in Io in general. --- In [email protected], "dennisf486" <dennisf486@...> wrote: > > 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? >