want access to arguments within the block of a macro #136

Open
HamletDRC opened this Issue Sep 1, 2011 · 4 comments

Projects

None yet

3 participants

@HamletDRC

I'd like to provide a String.eachChar method that expands out and lets
you execute a block for each char in the String.
My first revision is this:
macro def eachChar(value, &block)
quote {
value.toCharArray.each do | my_char |
block.body
end
}
end
eachChar('laat de leeeuw niet in zijn hempie staan') do | my_char |
puts my_char
end
This works... but only because the block parameter is called "my_char"
and the macro references the same "my_char" parameter name.

I want Mirah to provide a way to lookup the "my_char" parameter name and bind it into the macro.

@baroquebobcat
Member

I think what we really want here is to be able to do:

macro def each_char &block
  quote { toCharArray.each `block` }
end
@HamletDRC

I can imagine someone wanted access to the parameters though. For instance, maybe someone wants to modify the parameters somehow. How does Lisp solve this?

@baroquebobcat
Member

I don't know how Lisp deals with this.

You can access the params AST nodes through Block#args. So, in theory--it doesn't work currently--, you could do something like

quote do
   something_that_takes_a_block do |`block.args`|
    puts "Hey"
    `block.body`
   end
end

args is an AST::Arguments node, so you can get it's children etc.

@RobertFischer
Collaborator

Is there a resolution for this? I'd like to generate unit tests based on a pattern, and need access to the parameters just like Hamlet describes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment