In quick and dirty development/testing, the ability to just copy-paste code is very handy. If a snippet is used a lot, it can be elevated to a function later.
Is there a way to evaluate a file in local scope? The Base.include function evaluates in the global scope, which is not what I want. For a similar discussion, see Is "include" safe to use inside a function?
For example, say I have some complicated plotting routine in a file wowPlot.jl:
display(x^2)
I create some elaborate data and display it:
x = 5
include("wowPlot.jl")
25
Then I want a local scope to not mess with the original data:
Include into local scope, would be the same as eval into local scope. eval into local scope is one of the things that makes code impossible to optimize.
It is a feature julia very intentionally doesn’t have.
Yes, I love Julias compiled-language side. But I think interpreted, throwaway scripts are also very important for a modern language. I appreciate that some functions are optimized to the bitter end, but I would also like to have a interpreted interface that is more flexible. Like Python, perhaps.
Which is a close approximation to what C’s #include does.
I feel like it is a unwise thing, and I would say the exact same about putting a local #include in a C file.
Note that this not catch changes in the file included.
will do what you expect (i.e. act as if you pasted the included code at that point). (@oxinabox had the same idea and was a bit quicker to post, but I had a chance to test mine and I believe it works.)
(I wouldn’t recommend this from a maintenance/reability standpoint, however! And even for short-term hacks I think it is better to write functions as soon as you find yourself re-using code.)
The consensus seems to be that everything should be packaged into a function. I totally agree.
I will try to describe my use case without actually dumping the code here. I am building a plot that visualises 10 different things. The function would look like
This would not be a good function. I hope you agree. Currently in my script I use all those variables and parameters and build a nice plot. Then I modify some variables or some parameters and plot again and compare the difference. Currently I just copy-paste the plotting code to all places in all scripts where I need it. It would be cleaner to be able to simply raw-include into scripts. I you have solved this problem in some other way I would love to hear about it.
@include "testInclude.jl"
type QuoteNode has no field file
Stacktrace:
[1] getproperty(::QuoteNode, ::Symbol) at ./Base.jl:20
[2] @include(::LineNumberNode, ::Module, ::String) at ./In[1]:2nd_place_medal:
And then unpack inside the function? I think this is just syntax. The semantics of wowPlot are still the same and still too broad to be a good function.
Are you saying that I should casually write functions with more parameters that can fit on a line? My point is that I want a separation between good and proper functions on one hand and quick and dirty scripting on the other. This post is about making the scripting easier.