Re: [julia-users] macro: with
Heh... it took a google search to figure out where I put it. https://github.com/tbreloff/CTechCommon.jl/blob/master/src/macros.jl#L113-L155 On Sun, Oct 9, 2016 at 12:19 PM, Ben Lauwenswrote: > Hi Tom > > I am interested. I am building a macro mimicking how iterator blocks are > transformed in a state-machine in C# (as an alternative to Tasks) and this > is one of the (many) thinks I have to solve. Can you point me to the > source? Thanks > > Ben
Re: [julia-users] Macro as decorators
Possibly. Do you have a specific example in mind? That will probably get you a less ambivalent answer. :-) Cheers, Kevin On Monday, May 9, 2016, Ford Oxwrote: > Is it possible to write macro that will work as python decorator?
Re: [julia-users] Macro expansion for ccall with tuple argument
> and I programmed it as > julia> macro do_ccall(f,args...) > :(ccall(f,Int,$(ntuple(i->Int,length(args))),args...)) end I belive the arguments should be a tuple expression which you can generate with Expr(:tuple, [Int for i in 1:length(args)]...) > julia> macroexpand(:(@do_ccall(f,1,2,3))) > :(ccall(f,Int,(Int64,Int64,Int64),args...)) > In general you can use `Meta.show_sexpr` (or `dump`/`xdump`) to see more precisely what the expression is.
Re: [julia-users] Macro hygiene issue
Thank you, that solved it. On Wed, Mar 2, 2016 at 10:54 AM, Yichao Yuwrote: > On Wed, Mar 2, 2016 at 10:51 AM, Cedric St-Jean > wrote: > > Hi, I have a situation that looks a bit like this: > > > > module A > > function foo end > > end > > > > module B > > import A: foo > > macro define_foo(typ, value) > > :(foo(::$(esc(typ))) = $(esc(value))) > :(A.foo(...) = ...) > > end > > end > > > > module C > > using B: @define_foo > > @define_foo Int 19 > > end > > > > A.foo(20) > > > > In other words, A is defining a function foo, B defines a macro to define > > methods of foo, and C uses the macro. This looks like it should work, > but it > > doesn't, because hygiene takes over and `foo` gets replaced with a > gensym in > > its expansion, since it's on the left-hand-side of an equality. Is there > any > > way around this? >
Re: [julia-users] macro to exclude code depending on VERSION
ah, great. i won't make a new package then. thanks. On Thursday, 12 November 2015 09:30:21 UTC-3, Yichao Yu wrote: > > https://github.com/JuliaLang/julia/issues/7449 > https://github.com/JuliaLang/Compat.jl/pull/131 > https://github.com/JuliaLang/julia/issues/5892 > > On Thu, Nov 12, 2015 at 7:23 AM, andrew cooke> wrote: > > > > when you're writing code that uses macros, supporting different versions > of > > julia seems to be more complex than normal. in particular, things like: > > > > if VERSION > XX > > # code with macros here > > end > > > > don't work as expected, because macro expansion occurs before runtime > > evaluation. so the macros are expenaded whatever version. > > > > given that, i have found this simple macro to be useful; > > > > macro cond(test, block) > > if eval(test) > > block > > end > > end > > > > @cond VERSION >= v"0.4" begin > > # code with macros here > > end > > > > anyway, my questions are: (1) is the above sensible and (2) does this > > already exist? > > > > thanks, > > andrew > > >
Re: [julia-users] macro to exclude code depending on VERSION
https://github.com/JuliaLang/julia/issues/7449 https://github.com/JuliaLang/Compat.jl/pull/131 https://github.com/JuliaLang/julia/issues/5892 On Thu, Nov 12, 2015 at 7:23 AM, andrew cookewrote: > > when you're writing code that uses macros, supporting different versions of > julia seems to be more complex than normal. in particular, things like: > > if VERSION > XX > # code with macros here > end > > don't work as expected, because macro expansion occurs before runtime > evaluation. so the macros are expenaded whatever version. > > given that, i have found this simple macro to be useful; > > macro cond(test, block) > if eval(test) > block > end > end > > @cond VERSION >= v"0.4" begin > # code with macros here > end > > anyway, my questions are: (1) is the above sensible and (2) does this > already exist? > > thanks, > andrew >
Re: [julia-users] macro to exclude code depending on VERSION
...and I just discovered Requires.jl. Fantastic stuff. On Thursday, November 12, 2015 at 8:55:23 PM UTC-8, Seth wrote: > > This could be useful to me :) > > I have a couple of functions that require JuMP but I don't want to add > JuMP to my REQUIRE file. My usual tactic of checking isdefined(:JuMP) won't > work because JuMP uses macros that are evaluated prior to runtime. However, > I was unable to make the following code work: > > @cond (isdefined(:JuMP)) begin > function something_that_requires_jump(a...) > ... > end # function > end # macro > > Is there an accepted way to do conditional includes of packages that > contain macros? > > On Thursday, November 12, 2015 at 4:37:43 AM UTC-8, andrew cooke wrote: >> >> >> ah, great. i won't make a new package then. thanks. >> >> On Thursday, 12 November 2015 09:30:21 UTC-3, Yichao Yu wrote: >>> >>> https://github.com/JuliaLang/julia/issues/7449 >>> https://github.com/JuliaLang/Compat.jl/pull/131 >>> https://github.com/JuliaLang/julia/issues/5892 >>> >>> On Thu, Nov 12, 2015 at 7:23 AM, andrew cooke>>> wrote: >>> > >>> > when you're writing code that uses macros, supporting different >>> versions of >>> > julia seems to be more complex than normal. in particular, things >>> like: >>> > >>> > if VERSION > XX >>> > # code with macros here >>> > end >>> > >>> > don't work as expected, because macro expansion occurs before runtime >>> > evaluation. so the macros are expenaded whatever version. >>> > >>> > given that, i have found this simple macro to be useful; >>> > >>> > macro cond(test, block) >>> > if eval(test) >>> > block >>> > end >>> > end >>> > >>> > @cond VERSION >= v"0.4" begin >>> > # code with macros here >>> > end >>> > >>> > anyway, my questions are: (1) is the above sensible and (2) does this >>> > already exist? >>> > >>> > thanks, >>> > andrew >>> > >>> >>
Re: [julia-users] macro to exclude code depending on VERSION
This could be useful to me :) I have a couple of functions that require JuMP but I don't want to add JuMP to my REQUIRE file. My usual tactic of checking isdefined(:JuMP) won't work because JuMP uses macros that are evaluated prior to runtime. However, I was unable to make the following code work: @cond (isdefined(:JuMP)) begin function something_that_requires_jump(a...) ... end # function end # macro Is there an accepted way to do conditional includes of packages that contain macros? On Thursday, November 12, 2015 at 4:37:43 AM UTC-8, andrew cooke wrote: > > > ah, great. i won't make a new package then. thanks. > > On Thursday, 12 November 2015 09:30:21 UTC-3, Yichao Yu wrote: >> >> https://github.com/JuliaLang/julia/issues/7449 >> https://github.com/JuliaLang/Compat.jl/pull/131 >> https://github.com/JuliaLang/julia/issues/5892 >> >> On Thu, Nov 12, 2015 at 7:23 AM, andrew cookewrote: >> > >> > when you're writing code that uses macros, supporting different >> versions of >> > julia seems to be more complex than normal. in particular, things >> like: >> > >> > if VERSION > XX >> > # code with macros here >> > end >> > >> > don't work as expected, because macro expansion occurs before runtime >> > evaluation. so the macros are expenaded whatever version. >> > >> > given that, i have found this simple macro to be useful; >> > >> > macro cond(test, block) >> > if eval(test) >> > block >> > end >> > end >> > >> > @cond VERSION >= v"0.4" begin >> > # code with macros here >> > end >> > >> > anyway, my questions are: (1) is the above sensible and (2) does this >> > already exist? >> > >> > thanks, >> > andrew >> > >> >
Re: [julia-users] Macro definition and call environments
On Thu, Oct 15, 2015 at 8:28 AM,wrote: > - Do these global variables also refer to function calls like time(), > println() within a macro?? Yes, time and println are just const global bindings to generic function objects. I am kind of confused how to think of the hygiene of a macro: > - Should i be thinking in terms of what is returned as an expr from the > macro should not conflict with the code context where it is returned? > - Or should i be thinking in terms of what is being supplied to macro > should not conflict with what is inside the macro? I'm not sure what these two options mean but you should apply `esc()` to any expressions that should be interpreted as referring to things in the calling environment. Not sure if that helps.
Re: [julia-users] Macro to generate function signature
The reduced case is: h((x1::Int64,x2::Int64)...) = x1+1 Which doesn't work, because the ... conflicts with the vararg syntax (the error message is admittedly not very good). I would suggest to generate the entire function from your macro, in which case you can just splice the tuple in and everything should work. ps: using eval inside a macro, and parsing concatenated strings like that, can and should be avoided. Build Expr objects instead. On Thu, Apr 30, 2015 at 5:19 PM, David Gold david.gol...@gmail.com wrote: I'm working (in 3.7) on a function that takes two functions f and g as inputs, merges all non-conflicting methods of f and g into one function h, and returns h. I'm trying to use macros to generate the signatures of each method for h: macro argsgen(typle, n::Int64) y = eval(:($typle)) xn = symbol(x$n) return :( $xn :: $(y[n]) ) end macro repper(typle, n::Int64) ex = @argsgen($typle, 1) for i in 2:n ex = string(ex, , @argsgen($typle, $n)) end return parse(ex) end So, if f has a method with signature (x::Int64), then I can feed the 1-tuple '(Int64,)' to @argsgen, which generates the proper signature: julia ex1 = macroexpand( :(@argsgen((Int64,), 1) )) :(x1::Int64) and I can thereby define a method for h: julia h(@argsgen((Int64,), 1)) = x1 + 1 foobar (generic function with 1 method) julia h(1) 2 The problem is when I try to do this for a signature of more than one argument, say (Int64, Int64). Above I've tried implementing a macro that generates an expression of repeated '@argsgen' calls with the appropriate second argument: ex = macroexpand( :( @repper (Int64, Int64) 2 ) ) :((x1::Int64,x2::Int64)) which doesn't quite work when I attempt to use it for a signature: julia h(@repper (Int64, Int64) 2) = x1 + x2 ERROR: syntax: (x1::Int64,x2::Int64) is not a valid function argument name How can I get @repper to return an expression that isn't a tuple, but also has two @argsgen calls separated by a comma? I've tried a couple of other ways of doing this, but each expression wants to be returned as a tuple. And I can't find out how to make the tuple splat in such a way that works as a signature for h. Any thoughts? (Also, I imagine the tupocalypse will break this, but I can deal with that later.) Thank you all! David
Re: [julia-users] macro expansion for @until
You need to call esc on the expressions to provide macro hygiene. Sorry for the brief reply, but let me know if that helps. Btw, did you mean to include output from macroexpand above? If so, it doesn't seem to have gotten included. On Sun, Sep 14, 2014 at 9:28 PM, Zouhair Mahboubi zouhair.mahbo...@gmail.com wrote: New Julia user here :) Following the scipy/julia tutorial by D. Sanders and playing around with Macros http://nbviewer.ipython.org/github/dpsanders/scipy_2014_julia/blob/master/Metaprogramming.ipynb He has in there an example of macros. I was playing around with that and I ran into a case where I don't understand why the expanded code ends up introducing what seems to be a local variable. And this depends solely on whether `expr2` contains `i += 1` or `i = i + 1`. Can someone take the time to explain why i one case the macro expansion introduces a local variable? And how would one get around that in this case? Thanks, -z In [111]: macro until(expr1, expr2) quote #:( while !($expr1) # code interpolation $expr2 end #) end end In [122]: expr1 = quote i = 0 @until i==10 begin print(i) i += 1 end end; expr2 = quote i = 0 @until i==10 begin print(i) i = i + 1 end end; In [123]: eval(expr1) 0123456789 In [124]: eval(expr2) i not defined while loading In[124], in expression starting on line 1 in anonymous at In[122]:4 In [125]: macroexpand(expr1) Out[125]: quote # In[122], line 3: i = 0 # line 4: begin # In[111], line 4: while !(i == 10) # line 5: begin # In[122], line 5: print(i) # line 6: i += 1 end end end end In [126]: macroexpand(expr2) Out[126]: quote # In[122], line 11: i = 0 # line 12: begin # In[111], line 4: while !(#3677#i == 10) # line 5: begin # In[122], line 13: print(#3677#i) # line 14: #3677#i = #3677#i + 1 end end end end