Hi all, I am trying to teach myself macro. This is what i want to do.
anObject.aProperty1 = anInt
anObject.aProperty2 = anStr
anObject.aProperty3 = aBool
anObject.aProperty4 = anInt2
Run
But i want to write code like this- "With" is the name of my macro.
With anObject :
.aProperty1 = anInt
.aProperty1 = aStr
.aProperty1 = aBool
.aProperty1 = anInt2
Run
So i want to tell compiler, if see this peice of code, please convert and
expand it to the above style. I know that some packages are there to achieve
this but i would like to learn by doing. So far, i wrote a macro and loop
through the body variable. But I can only get the i.kind. See this -
macro with(obj : typed, body : untyped) : untyped =
var bn = body
bn.expectKind(nnkStmtList)
for i in bn :
echo i. kind
Run
1. What i want to do in this loop. - Get each line of code and combine it
with "obj". But i can't get each line of code in this loop.
2. This macro wanted me to add a return type. Is it necessary ? I mean I am
assuming that i am going to tell the compiler to treat this code as i intended.
Expand it as i said. So do i really need a return type ? I know this question
seems to be a stupid one, but I don't know actually what is happening inside a
macro.