Maybe something like this should work:

julia> macro A()
       esc(quote
       a=5
       b=7
       end)
       end

julia> function f(x)
       @A
       x+a+b
       end
f (generic function with 1 method)

julia> f(5)
17

If you don't want your variable names to be mangled then do the `esc`.

So, basically just write normal code in the `esc(quote ... end)` bit.

On Thu, 2014-06-26 at 19:21, [email protected] wrote:
> That explains it, thanks.
>
> In my actual problem, what I wanted to do in the included file was 
> something like this:
>
> ```
> sall,sloops,slost,Nbins,psibins,initialhist,finalhist,vols = 
> jldopen("$datafile.jld") do f
>         read(f, "sall"),
>         read(f, "sloops"),
>         read(f, "sloss"),
>         read(f, "Nbins"),
>         read(f, "psibins"),
>         read(f, "initialhist"),
>         read(f, "finalhist"),
>         read(f, "vols")
> end
> ```
>
> where `datafile` is the variable defined in the function. In other words, 
> including the file would define and assign to all those variables. Is it 
> maybe possible to write a macro that does this?
>
> // T
>
> On Thursday, June 26, 2014 8:17:29 PM UTC+2, Simon Kornblith wrote:
>>
>> include evaluates at top-level, so this would only work if foo were a 
>> global variable. It not possible to include in a function context for the 
>> same reason it is not possible to eval in a function context.
>>
>> Simon
>>
>> On Thursday, June 26, 2014 1:03:00 PM UTC-4, Tomas Lycken wrote:
>>>
>>> I have the following two files:
>>>
>>> *includetest1.jl*:
>>>
>>> module IncludeTest
>>>
>>> function testinclude()
>>>     foo = "foo"
>>>     println(foo)
>>>     include("includetest2.jl")
>>> end
>>>
>>> end
>>>
>>> *includetest2.jl*
>>>
>>> println(foo)
>>>
>>> If I now try to execute this the function from the REPL, I get errors 
>>> stating that foo is not defined:
>>>
>>> julia> include("includetest1.jl")
>>>
>>> julia> IncludeTest.testinclude()
>>> foo
>>> ERROR: foo not defined
>>>  in include at boot.jl:244
>>> while loading [...]/includetest2.jl, in expression starting on line 1
>>>
>>> I thought include was supposed to just insert the contents of the file 
>>> in whatever context you’re in? If include is not the way to do this, is 
>>> there another?
>>>
>>> For completeness:
>>>
>>>
>>> julia> versioninfo()
>>> Julia Version 0.3.0-prerelease+3884
>>> Commit 3e6a6c7* (2014-06-25 10:41 UTC)
>>> Platform Info:
>>>   System: Linux (x86_64-linux-gnu)
>>>   CPU: Intel(R) Core(TM) i7-2630QM CPU @ 2.00GHz
>>>   WORD_SIZE: 64
>>>   BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY)
>>>   LAPACK: libopenblas
>>>   LIBM: libopenlibm
>>>
>>> // T
>>> ​
>>>
>>

-- 

Reply via email to