Skip to content

Instantly share code, notes, and snippets.

@TsurHerman
Created May 31, 2018 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TsurHerman/2ec94d5bc6a361bef79f74aa71aa2740 to your computer and use it in GitHub Desktop.
Save TsurHerman/2ec94d5bc6a361bef79f74aa71aa2740 to your computer and use it in GitHub Desktop.
Help with a macro
abstract type ContextCall{FN,MOD,C} end
abstract type Context{C} end
Context{C}(x) where C = x
# desired output for macro
# @mfunction AAA(a::Int64,b::Int64) = a+b
#
@generated ContextCall{:AAA,:Main,C}(a::Int64,b::Int64) where C = begin
code = quote
a+b
end
Context{C}(code)
end
ContextCall{:AAA,:Main,Any}(1,2) #3
################################################
#my groping in the dark
using MacroTools
using MacroTools: combinedef
D = splitdef(:(AAB(a::Int64,b::Int64) = a+b))
mod = current_module()
MOD = module_name(mod)
FN = D[:name]
D[:name] = Symbol("ContextCall{:$FN,:$MOD,C}")
D[:whereparams] = (:C,)
body = deepcopy(D[:body])
body = "code = quote $body end;Context{C}(code)"
D[:body] = parse(body)
A = sprint(print,:(@generated $(combinedef(D))))
parse(A)
eval(parse(A))
ContextCall{:AAB,:Main,Any}(1,2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment