Thanks, I had to cancel the hygiene too.
On Monday, January 18, 2016 at 1:45:23 PM UTC-8, Cedric St-Jean wrote:
>
> We're missing your `decompose_function` so I couldn't run it, but from the
> last time I worked with function-definition macros, I believe the correct
> way to write it should be to add `esc` to the arguments so that it doesn't
> apply hygiene, this way:
>
> function $(esc(f))($(map(esc, args)...))
>
> Unfortunately, last I checked there was a bug in Julia that prevented
> this, and instead I have to cancel hygiene on the whole expression (and use
> `gensym` where appropriate)
>
> esc(:(function $f($(args...))
> ...)
>
> Does that solve your issue?
>
> On Monday, January 18, 2016 at 2:33:00 PM UTC-5, Julia Tylors wrote:
>>
>> #####################
>> #IMPLEMENTATION CODE:
>> #####################
>>
>> module Y
>> export modify
>>
>> """
>> A LOT OF CODE HERE, REMOVED FOR SIMPLICITY
>> """
>>
>> function _modify(expr)
>> is_lambda,f,args,body = decompose_function(expr)
>> if is_lambda
>> quote
>> ($(args...)) -> $(transform(body))
>> end
>> else
>> quote
>> function $(esc(f))($(args...))
>> $(transform(body))
>> end
>> end
>> end
>> end
>>
>> macro modify(func)
>> _modify(func)
>> end
>> end
>>
>> #####################
>> #USAGE CODE:
>> #####################
>>
>> module X
>> using Y
>> type Arg{T}
>> x::T
>> end
>> println(macroexpand( quote
>> @modify function f(x::Arg{Bool})
>> y = 12
>> if x
>> y+1
>> else
>> y-1
>> end
>> end
>> end))
>> end
>>
>> #####################
>> #GENERATED CODE:
>> #####################
>>
>> begin
>> begin
>> function f(#22#x::Y.Arg{Y.Bool})
>> begin
>> #21#y = 12 #
>> begin
>> "LOTS OF CODE HERE"
>> end
>> end
>> end
>> end
>> end
>>
>> #########################
>> #QUESTION
>> #########################
>>
>> In the generated code, the argument Arg{Bool} is treated as if it is an
>> element of module Y, but it is an element of module Y. So when the code
>> executes, the interpreter complains and says:
>> ERROR: LoadError: UndefVarError: Arg not defined
>>
>> Why $(args...) in the implementation code doesn't work?
>>
>> Thanks
>>
>>
>>