Just to be clear: I’m aware that there’s probably no way to get this 
working with e.g. any string variable. But I don’t see a reason why it 
shouldn’t be possible when passing a constant, or especially a literal 
constant, to the macro, such as with @foo("bar") (rather than bar="bar"; 
@foo(bar), which I understand might be impossible). 

// T

On Thursday, June 26, 2014 11:54:55 PM UTC+2, Tomas Lycken wrote:

This gets me quite far - thanks a lot! However, to make it work, I have to 
> hard-code the value of datafile, since I can’t figure out how to get the 
> macro to create the string "$datafile.jld" correctly. This is probably OK 
> for my use case - I only have two datafiles, so I can create two macros :P 
> But it would be nice to learn how to do it, if it’s possible.
>
> Basically, is there a way to make the following work?
>
> @macro foo(bar)
>     esc(quote 
>         "$bar.baz"
>     end)
> end
>
> Currently @foo("fizz") is just the string literal "$bar.baz".
>
> // T
>
> On Thursday, June 26, 2014 9:25:18 PM UTC+2, Mauro wrote:
>
> 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