macros - Using injected values -


i'm trying use injected values like:

quote   var!(state) = "something"   unquote(block)   do_something_else_with(state) end 

i know it's evil, want fun. possible access state after block may or may not have done it?

yes, block , do_something_else_with both need use var!(state) macro hygiene doesn't end giving state different name in resulting code. here's example:

defmodule main   defmacro with_state(do: block)     quote       var!(state) = "something"       io.inspect {:before, var!(state)}       unquote(block)       io.inspect {:after, var!(state)}     end   end    def main     with_state       io.inspect {:inside_before, var!(state)}       var!(state) = "else"       io.inspect {:inside_after, var!(state)}     end   end end  main.main 

output:

{:before, "something"} {:inside_before, "something"} {:inside_after, "else"} {:after, "else"} 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -