macro store(agent, value)
f_name = Symbol("store_", value)
f = quote
@inline @generated function $f_name(a, v)
return quote a.value = v end
end
end
eval(f)
return :($f_name($agent, $value))
end
The idea is that the macro creates a generated function(it needs to be generated as if the agent does not include that field then it returns :(nothing), I did not include that part) to store the value in the agent. Unfortunately, I cannot seem to get the line:
return quote a.value = v end
to work either value is there or the data that is stored in value at call(ie [100]). I think that is because it is a quote within a quote. Does anybody know how to achieve that?
I did an experiment where I was checking hasfield() in a for loop and it was noticeably slower. Do you know what triggers constant propagation in this case?
Thank you for answering. You seem very knowledgeable about Julia would you happen also to know how to fix the above code, for future reference?
The symbol you are checking just needs to be fixed within a function (it can be fixed before and then fed in, it just has to be constant somewhere close enough for the compiler to use it), then the compiler can check the type for the field beforehand. If you pass it in dynamically as a Symbol argument, that doesnβt work. All places where you write obj.field in some code this is the same as writing getfield(obj, :field) so that kind of constant propagation / folding happens all the time.