Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

returning a metamethod from a macro #66

Open
Qiyamah opened this issue Jul 30, 2018 · 1 comment
Open

returning a metamethod from a macro #66

Qiyamah opened this issue Jul 30, 2018 · 1 comment

Comments

@Qiyamah
Copy link

Qiyamah commented Jul 30, 2018

	function gen_sym(pattern,body)
		quote
    		@metafunction testfun($pattern) begin
    		    $(body...)
    		end
    	         end
	end

	@macromethod test(pattern,begin *{body} end) begin
	    pattern_tree = analyze(pattern,current_module()).child
	    #matchfun = matcher(pattern,@currentmodule)
	    if pattern_tree.head.sym != :call
	    	error("not a function call")
	    end
	    fun = gen_sym(pattern,body)
    	quote
    		$fun
    	end
	end

macroexpand(:(@test f(:E{x,2},:T{y,Int}) begin
                           f(x+y)
                     end))

results in

:($(Expr(:error, MethodError(haskey, (ExpressionPatterns.Dispatch.TopMetaTables.MetaModuleTable(Dict{Symbol,ExpressionPatterns.Dispatch.Structure.MetaMethodTable}(), Set{Symbol}()), :($(Expr(:globalref, Main, :testfun)))), 0x0000000000005886))))

I can't see why?
Why can't i return a metafunction?

thanks

@fcard
Copy link
Owner

fcard commented Jul 30, 2018

@metafunction and @macromethod only work in the global scope, because they need to call eval. I will see if I can do something about this.. (I am about two thirds done with the slurp refactoring I've been meaning to do, than I will take a look at all the other issues)

Meanwhile, see if you can't use ExpressionPatterns.Destructuring to solve your problem. If you don't need dispatch, you can just use @funds.

function gen_sym(pattern,body)
  quote
    @funds testfun($pattern) = begin
        $(body...)
    end
  end
end

(mind the =, which isn't optional in @funds)

If need be, you can combine it with ExpressionPattern.Matching to create a manual dispatch that doesn't affect the global state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants