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

anonymous scopes for including files? #3661

Closed
mlubin opened this issue Jul 10, 2013 · 8 comments
Closed

anonymous scopes for including files? #3661

mlubin opened this issue Jul 10, 2013 · 8 comments

Comments

@mlubin
Copy link
Member

mlubin commented Jul 10, 2013

The test suite for MathProg has a set of files which I'd like to run in isolated scopes (one of the tests is for macro hygiene). What I'd like to say is something like:

let; include("test.jl"); end

where test.jl has some using statements. However, included modules "leak" between files, probably because using is a top-level statement. I can't find a reasonable way to do this. Other options include:

  • evalfile -- Backtraces are broken (they don't give a line number in the included file), so not great for a test suite.
  • Wrap each test file in a named module. This works but is ugly. Anonymous modules would be reasonable here.
  • @loladiro suggested the following trick:
m = Module(:__anon__)
body = Expr(:toplevel,:(ARGS=[]),:(include($testfile)))
eval(m,body)

This doesn't quite work; if testfile contains:

require("MathProg")

then for some reason, the module MathProg still won't be in scope.

There are a few different ways this could be done. I'm just asking for at least one that works nicely.

@JeffBezanson
Copy link
Member

I would expect using MathProg instead of require to work in the last method.

@mlubin
Copy link
Member Author

mlubin commented Jul 10, 2013

using MathProg does work with the last method, but in my macro hygiene test I just want to require, not use. Is it a bug that this doesn't work?

@JeffBezanson
Copy link
Member

You can do import MathProg and that will give just the reference to the module without using its contents.

@mlubin
Copy link
Member Author

mlubin commented Jul 21, 2013

Good point. Could we have a nicer syntax for this:

m = Module(:__anon__)
body = Expr(:toplevel,:(ARGS=[]),:(include($testfile)))
eval(m,body)

@JeffBezanson
Copy link
Member

That's what evalfile does.

@Keno
Copy link
Member

Keno commented Jul 21, 2013

Not quite. There's the subtle difference that evalfile gives crappy backtraces and returns the last thing that was evaluated.

@mlubin
Copy link
Member Author

mlubin commented Jul 21, 2013

The backtrace issue is the biggest one for me, but maybe that should be a separate issue.

@mlubin
Copy link
Member Author

mlubin commented Jul 27, 2013

Fixed by b97e7cf

@mlubin mlubin closed this as completed Jul 27, 2013
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