UndefVarError with nested function passed to @cfunction

I’m working on a project that involves wrapping Julia functions to be used as callbacks for a C library. I don’t understand the error when I run this, but I’m new enough to Julia to be confused about whether I should expect this to work:

function f()
    function g()::Cvoid
        return nothing
    end
    g_c = @cfunction(g, Cvoid, ());
end

I see UndefVarError: g not defined with Julia 1.5.2 running in a Jupyter notebook.

Notes:

  • I see some related posts on a similar behavior with a conditionally-defined nested non-anonymous function, and this open issue.
  • The actual “g” I’m interested in is more complex, with input arguments and modifying an array defined in f.

g_c = @cfunction($g, Cvoid, ());

doc

1 Like