this is my first ever post on here so please don’t mind if I do something wrong.
Currently I am trying to wrap my head around macros and how to use them and what is possible and what is not. So what I want to have is a variable
expr storing some expression and having a while loop looping over the statement stored in this variable. So a small example with a macro called
@iterate could look like this.
expr = :(i<5) i=0 @iterate while expr i += 1 println(i) end 1 2 3 4
So I had much trouble to get this working somehow. With this code I could get it to work, but I am not totally satisfied with this solution.
macro iterate(expr::Expr) conditions = eval(quote quote $$(expr.args) end end) statements = expr.args return esc(quote while $conditions $statments end end) end expr = :(i<5) i=0 @iterate while expr global i += 1 println(i) end
So I do have some questions:
- Does the definition of
conditionshave to be so difficult and somehow ugly?
- I do not like the
global i +=1. Is there a way to do this without the global?
- Is the idea of my implementation the best one or is there an easier way to do it?
Sorry, if this is somehow trivial. I really tried to look at many threads and macro implementations, but somehow got confused on the way and only could come up with this implementation .
Thanks for answers in advance and have a nice day