-
Notifications
You must be signed in to change notification settings - Fork 21
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
Error @yield macro outside a @resumable function!
when using @yield inside a macro
#55
Comments
When i write it out manually, things work as expected:
|
What is |
There are a couple of issues here involving using macros within But I think a PR adding something like this could work: function expand(M, ex::Expr)
head = ex.head
args = ex.args
if head == :macrocall && args[1] != Symbol("@yield")
return expand(M, macroexpand(M, ex; recursive=false))
else
newargs = map(Base.Fix1(expand, M), ex.args)
return Expr(ex.head, newargs...)
end
end
expand(M, s) = s With your example, you'd then get julia> macro yieldmany(resumable::Expr)
return quote
for x in $(esc(resumable))
@yield x
end
end
end
@yieldmany
julia> ex = quote
function F2()
@yield 2
@yieldmany F1()
@yield 4
end
end;
julia> expand(Main, ex)
quote
#= REPL[174]:2 =#
function F2()
#= REPL[174]:2 =#
#= REPL[174]:3 =#
#= REPL[174]:3 =# @yield 2
#= REPL[174]:4 =#
begin
#= REPL[173]:3 =#
for var"#889#x" = F1()
#= REPL[173]:4 =#
#= REPL[173]:4 =# @yield x
#= REPL[173]:5 =#
end
end
#= REPL[174]:5 =#
#= REPL[174]:5 =# @yield 4
end
end It's not quite right - the variables in the |
I wanted a macro that takes a sub-generator and yields its elements. I wrote this:
However, when I use it in a function, I get an error:
I tried using macroexpand to see what it's making, but I get the same error:
The text was updated successfully, but these errors were encountered: