Thanks Simon, I get the correct behavior following your example. But I
don't understand why are my macros looking for the variable (pdbml) in my
module (MIToS.PDB.pdbml):
julia> using MIToS.PDB
julia> macro example(var)
:(dump($var))
end
julia> let code = "1SSX"
pdbml = read(".ju", PDBML)
println(macroexpand(:(@example(pdbml))))
println(macroexpand(:(@residues pdbml model "*" chain "*" residue
"141")))
end
.julia/ .julia_history .jupyter/
julia> let code = "1SSX"
pdbml = read(".julia/v0.4/MIToS/test/data/1SSX.xml", PDBML)
println(macroexpand(:(@example(pdbml))))
println(macroexpand(:(@residues pdbml model "*" chain "*" residue
"141")))
end
dump(pdbml)
MIToS.PDB.collectobjects(MIToS.PDB.pdbml,MIToS.PDB._residues_tests("*","*",
"141")...)
I don't use any* eval()* call in my macro, the code is (residue_list gives
me the poblem):
macro residues(residue_list,
model::Symbol, m,
chain::Symbol, c,
residue::Symbol, r)
if model == :model && chain == :chain && residue == :residue
return :(collectobjects($residue_list, _residues_tests($m, $c, $r)...))
else
throw(ArgumentError("The signature is @residues ___ model ___ chain ___
residue ___"))
end
end
El sábado, 12 de septiembre de 2015, 19:27:06 (UTC-3), Simon Danisch
escribió:
>
> Well, m is not a global because of the let blog. Because you evaluate that
> statement, m needs to be in the scope of the macro, which it is only for
> msg (msg is global).
> You probably don't want to eval in a macro, and just return an expression
> instead. That's what macros do, take an expression and replace that
> expression at compile time.
> e.g.
> macro example(var)
> :(dump($var))
> end
>
> So this:
> let m = "hola"
> @example m
> end
> will be expanded at compile time to:
> let m = "hola"
> dump(m)
> end
> so they're in the same scope now.
> You might find macroexpand useful: macroexpand(:( @mymacro my_expr)))
>
> Am Samstag, 12. September 2015 23:45:23 UTC+2 schrieb Diego Javier Zea:
>>
>> I don't understand why am I getting this error? My macros can not find
>> the variables inside *let* blocks. How Can I fix it? Thanks in advance
>>
>> julia> macro example(var)
>> dump(var)
>> @eval dump($var)
>> end
>>
>> julia> msg = "hola"
>> "hola"
>>
>> julia> @example msg
>> Symbol msg
>> ASCIIString "hola"
>>
>> julia> let m = "hola"
>> @example m
>> end
>> Symbol m
>> ERROR: UndefVarError: m not defined
>> in eval_user_input at REPL.jl:64
>> [inlined code] from REPL.jl:93
>> in anonymous at task.jl:68
>>
>>
>>
>>
>>