Re: "do" blocks and method args problem

Jeremy Tregunna <[email protected]> Thu, 12 Apr 2012 22:13:00 -0600
Newsgroups gmane.comp.lang.io
Message-ID <[email protected]>
The way I'd probably go about doing this is something like this:

method(
  self ldMap := Object clone
  call message arguments foreach(arg,
    self ldMap appendProto(call sender doMessage(arg))
  )
)

This wouldn't be needed if do() was lexical, which I believe can be implemented like this (warning, not tested):

Object lexicalDo := block(
  call argAt(0) doInContext(call target, self)
  call target
) setIsActivatable(true)

Try it and see. The first code snippet will do what you want though.  

Regards,

Jeremy Tregunna


On Thursday, 12 April, 2012 at 10:07 PM, 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 (http://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 (http://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?
>  
>