julia> function distance(point::Point)c x = point.x y = point.y sqrt(x*x + y*y) endHow about this?julia> function distance(point::Point)
julia> type Point x y end
julia> macro fetch(p) variables = quote x = $p.x y = $p.y end return esc(variables) end
julia> p = Point(5, 10)Point(5,10)
julia> xERROR: x not defined
julia> yERROR: y not defined
julia> @fetch p;
julia> x5
julia> y10Oh, yes I see that now. Then what you could do is quote the call to names() in your macro expression so the names call is executed in the calling environment.macro fetch(p)return quotelocal vars = names($p)local ex = Expr(:block)append!(ex.args, [:($v = $p.$v) for v in vars])eval(ex)endend
This is similar to what yo have done at the REPL but it quotes the names() call so it can get evaluated in the macro calling environment instead of within the macro itself. ex = Expr(:block) is just another quote block with a list of expressions to evaluate in ex.args. Note that there is no need to use esc() here because the eval() call happens in the calling environment.
x = point.x
y = point.y
# do stuff on x and y
point.x = x
point.y = y@fetch_and_store(Point, :foo, quote
#do stuff on x and y
end)
p = Point(1, 0)
foo(p)
const circuitModelNames = names(CircuitModel)
macro fetch(ex)
Expr(:block, [ :($(circuitModelNames[i]) = $ex.$(circuitModelNames[i]) ) for i = 1:length(circuitModelNames) ]...) |> esc
end
julia> macroexpand (:(@fetch model))
:(begin
v1 = model.v1
v5 = model.v5
v7 = model.v7
v2 = model.v2
v3 = model.v3
v4 = model.v4
v9 = model.v9
v12 = model.v12
v15 = model.v15
v18 = model.v18
v19 = model.v19
v20 = model.v20
v21 = model.v21
v8 = model.v8
v22 = model.v22
v23 = model.v23
v24 = model.v24
i1 = model.i1
i2 = model.i2
i3 = model.i3
i4 = model.i4
i5 = model.i5
i6 = model.i6
i7 = model.i7
ic1eq = model.ic1eq
ic2eq = model.ic2eq
ic3eq = model.ic3eq
ic4eq = model.ic4eq
ic5eq = model.ic5eq
ic6eq = model.ic6eq
ic7eq = model.ic7eq
ic8eq = model.ic8eq
sr = model.sr
srinv = model.srinv
pi = model.pi
gmin = model.gmin
is1 = model.is1
nvtf1 = model.nvtf1
is2 = model.is2
nvtf2 = model.nvtf2
nvtinvf1 = model.nvtinvf1
vcritf1 = model.vcritf1
nvtinvf2 = model.nvtinvf2
vcritf2 = model.vcritf2
gc1 = model.gc1
gr1 = model.gr1
gr2 = model.gr2
itxr2 = model.itxr2
gc2 = model.gc2
gc3 = model.gc3
gc4 = model.gc4
gc5 = model.gc5
gr7 = model.gr7
gc6 = model.gc6
gr3 = model.gr3
itxr3 = model.itxr3
gc7 = model.gc7
gr4 = model.gr4
gc8 = model.gc8
gr5 = model.gr5
vpos = model.vpos
vneg = model.vneg
gin = model.gin
gininv = model.gininv
vposcap = model.vposcap
vnegcap = model.vnegcap
ginotacore = model.ginotacore
ginotares = model.ginotares
ginotacoreinv = model.ginotacoreinv
ginotaresinv = model.ginotaresinv
vc3lo = model.vc3lo
vc3hi = model.vc3hi
a4a4c = model.a4a4c
a5a5c = model.a5a5c
a6a6c = model.a6a6c
a14a14c = model.a14a14c
a16a16c = model.a16a16c
a17a17c = model.a17a17c
a17a17nrmc = model.a17a17nrmc
a12a17c = model.a12a17c
a16a16nrmc = model.a16a16nrmc
a15a16c = model.a15a16c
a14a14nrmc = model.a14a14nrmc
a13a14c = model.a13a14c
a6a14c = model.a6a14c
a13a6c = model.a13a6c
a5a5nrmc = model.a5a5nrmc
a4a5c = model.a4a5c
a2a5c = model.a2a5c
a2a4c = model.a2a4c
a4a4nrmc = model.a4a4nrmc
a1a4c = model.a1a4c
v5c = model.v5c
v7c = model.v7c
end)
macro fetch(name, typ) _typ = eval(typ) r = Expr(:block) r.args = [:($n = $name.$n) for n in _typ.names] return esc(r)end
macroexpand(:(@fetch model CircuitModel))