Coder Social home page Coder Social logo

Comments (3)

cscherrer avatar cscherrer commented on June 23, 2024 1

There are a couple of issues here involving using macros within @resumable. Usually, a quick fix would be to have @resumable call macroexpand on the expression before rewriting things. The problem with that is that there's not actually a working @yield anywhere. In a way, it's not really a macro, more of a syntactic indicator the rewrite engine looks at. So macroexpand will see a @yield and get very confused.

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 for loop got mixed up - but I think something along these lines could be made to work.

from resumablefunctions.jl.

wmanning-cra avatar wmanning-cra commented on June 23, 2024

When i write it out manually, things work as expected:

@resumable function F2()
          @yield 2
          for i in F1()
             @yield i
           end
          @yield 4
end
julia> collect(F2())
4-element Array{Any,1}:
 2
 1
 3
 4

from resumablefunctions.jl.

hdavid16 avatar hdavid16 commented on June 23, 2024

What is F1? I've seen weird things happen when trying to call macros inside a quote block (https://discourse.julialang.org/t/error-handling-in-quote-block-using-jump/79362/3?u=hdperez). I would discourage doing this.

from resumablefunctions.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.