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

Dynamically generating a module fails on master (sometimes on 0.2) #5276

Closed
bfredl opened this issue Jan 1, 2014 · 4 comments
Closed

Dynamically generating a module fails on master (sometimes on 0.2) #5276

bfredl opened this issue Jan 1, 2014 · 4 comments

Comments

@bfredl
Copy link
Contributor

bfredl commented Jan 1, 2014

I'm experimenting a bit with auto-generating bindings to Gtk3/Clutter/etc using GObject-introspection in a fork of Gtk.jl (https://github.com/bfredl/Gtk.jl )

My current approach depends on dynamically generating modules and I'm using

module GI
    function create_module(modname::Symbol)
        eval(quote module ($modname) end; $modname end)
    end
    setconst(mod,name,val) = eval(mod, :(const $name = $(Expr(:quote, val))))
end

However, create_module(:MyMod) fails on Julia master (syntax error in eval), and crashes non-deterministically on 0.2 stable. Actually my test cases running inside Gtk.jl mostly never crashes, but the above code usually segfaults when run on its own in a fresh interactive 0.2 session. (under arch linux 64bit)

is there a better way to dynamically generate a module (and populate it with constants, etc)? Or maybe I should just wait for #1974 ?

@JeffBezanson
Copy link
Member

I wouldn't generate the module by trying to poke at it externally with eval, but rather by using metaprogramming within the generated module. For example, see base/linalg/lapack.jl, which uses macros and @eval to generate wrappers.

Generating top-level expressions with eval is tricky. This works (although not recommended):

function create_module(modname::Symbol)
    eval(Expr(:toplevel, :(module ($modname) end), modname))
end

@bfredl
Copy link
Contributor Author

bfredl commented Jan 2, 2014

Thanks! But eval( Expr(:toplevel, :(module MyMod; const myconst = var ...; end))) seems to sometimes put the constants in the parent module instead (on master). I'll try to isolate a test case later. (I understand why you recommend against it, #1974 will be better solution if it gets implemented)

@bfredl
Copy link
Contributor Author

bfredl commented Jan 2, 2014

sorry, that was on 0.2 (mixing up my julia executables), on master it works all fine. Thanks!

@bfredl bfredl closed this as completed Jan 2, 2014
bfredl added a commit to bfredl/Gtk.jl that referenced this issue Jan 2, 2014
@filmackay
Copy link

To create a module in the root namespace use eval(Main, ...). Didn't know about Main, there you go..

module A
    function create_module(modname::Symbol)
        Expr(:toplevel, :(module ($modname) end))
    end

    e = A.create_module(:B)
    println(e)
    eval(Main, e)
end

B

#$(Expr(:toplevel, :($(Expr(:module, true, :B, :(begin 
#        eval(x) = top(Core).eval(B,x)
#        eval(m,x) = top(Core).eval(m,x)
#    end)))), :B))
#
# B

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

No branches or pull requests

3 participants