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

Added @softscope macro for ease of use #1

Merged
merged 6 commits into from
Sep 3, 2018
Merged

Added @softscope macro for ease of use #1

merged 6 commits into from
Sep 3, 2018

Conversation

dawbarton
Copy link
Contributor

The title says it all really. Thanks for making this package.

stevengj
stevengj previously approved these changes Aug 27, 2018
@stevengj
Copy link
Member

LGTM, once the merge conflict is fixed.

@dawbarton
Copy link
Contributor Author

Great, I'll have a look at it tomorrow. (I might switch around how the escaping is handled since I'm not quite happy with it.)

```
"""
macro softscope(ast)
esc(softscope(__module__, ast))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't __module__ new in Julia 0.7 (JuliaLang/julia#22064)? SoftScope.jl has to support 0.6, where I guess you would use current_module().

@stevengj
Copy link
Member

Needs a test.

@stevengj stevengj dismissed their stale review August 28, 2018 17:01

needs test and 0.6 compatibility

@dawbarton
Copy link
Contributor Author

Working on it; consider this a WIP for the moment.

@dawbarton
Copy link
Contributor Author

OK, all done. Should be v0.6+ compatible and has tests.

@Datseris
Copy link

Sorry to invade this PR, but I think it is better to ask here than to open a new issue.
In JuliaLang/julia#28789 it was proposed that the following is possible:

julia> a = 0                                                                
0                                                                           
                                                                            
julia> for i = 1:10                                                         
         a += i                                                             
       end                                                                  
ERROR: UndefVarError: a not defined                                         
Stacktrace:                                                                 
 [1] top-level scope at .\REPL[2]:2 [inlined]                               
 [2] top-level scope at .\none:0                                            
                                                                            
julia> using SoftGlobalScope                                                
[ Info: Precompiling SoftGlobalScope [363c7d7e-a618-11e8-01c4-4f22c151e122] 
                                                                            
julia> for i = 1:10                                                         
         a += i                                                             
       end                                                                  
                                                                            
julia> a                                                                    
55  

i.e. the @softscope macro introduced here is applied everywhere by default. Is this still possible to be done?

@dawbarton
Copy link
Contributor Author

Hi @Datseris, see #2 for that request. I looked into it further this afternoon and I have a few suggestions for a way forward (I'll write up in the next few days). In the meantime, I've updated the gist at https://gist.github.com/dawbarton/0388715fb56fb5cd05e0e4b12c322815 which implements my first attempt at the REPL hooks. Note the comment in the code if you use Revise! (Don't try putting the code in your startup.jl; it won't work there at the moment.)

@stevengj
Copy link
Member

stevengj commented Aug 29, 2018

I modified the test to make sure it works with a variable that is defined in the @softscope block. I don't think it will at the moment— this is why softscope_include_string has to evaluate one statement at a time.

@dawbarton
Copy link
Contributor Author

Two can play at that game! 😄

Try this with softscope_include_string -

softscope_include_string(Main, "if true ; ab = 0; for i = 1:10; ab += i ; end ; end")

I think we'll need to recurse into blocks and record new assignments rather than just trying to execute one block at a time (which will obviously fail here). I've got a stack of exam marking to do by close of play tomorrow so I won't be able to have a look before then but I'll have a go at it after that if you haven't already had a look at it.

@stevengj
Copy link
Member

stevengj commented Aug 29, 2018

@dawbarton, yes, that occurred to me. It's not high on my priority list at the moment since I'm guessing that defining new global variables in blocks is not so common in interactive code, but we can definitely catch some common cases like x = .... assignments in begin and if blocks.

However, without that feature I suspect that @softscope is much less useful, since @softscope begin ... end is probably the most useful formulation.

(It will be possible to fool softscope with things like @eval, but that's even further down on my list of priorities.)

@dawbarton
Copy link
Contributor Author

This should now all work correctly with #4 merged.

@stevengj stevengj merged commit da50d45 into JuliaLang:master Sep 3, 2018
@dawbarton dawbarton deleted the macro branch September 5, 2018 13:18
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

Successfully merging this pull request may close these issues.

3 participants