-
Notifications
You must be signed in to change notification settings - Fork 143
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
conditionally @load non-existent file #83
Comments
If you have any suggestions, I'm all ears. There is no way to:
Unlike Matlab, Julia doesn't allow one to assign variables in another workspace. |
Hmm, it doesn't look like JLD provides a function to read all variables into a Dict. Would that make you happier? You'd have to extract them manually, of course. |
i've been trying to come up with a solution and am stumped. WHY DOES THIS WORK: julia> macro set_var(var_name,var_value) julia> @set_var "foo" 7 julia> foo AND NOT THIS: julia> var_name="bar" julia> var_value=8 julia> sym = esc(symbol(var_name)) julia> ex=:($sym = $var_value) julia> eval(ex) |
which indeed works, but doesn't really help here. |
Another possibility is to make this: @load file begin
...
end Then instead of determining the names of the variables from the contents of the file at compile time, you can determine the variables that are undefined at use in the code block at compile time, and then see if you can read them out of the file at runtime. I'm not sure if this is really worth it, though; the syntax is still kind of ugly. |
what is the reasoning behind julia not allowing one to assign variables in another workspace? |
Julia's functions are quite unlike MATLAB's. In MATLAB, a function scope has an explicitly stored "workspace" that is effectively a Dict{Symbol,Any}. In Julia, running functions really don't have a workspace. At compile time, the compiler finds a place on the stack or a CPU register for every variable inside a given function at compile time, and the running code references those locations. (If it knows the types of the variables, the compiler generates code that stores and operates on the variables more efficiently than it otherwise could.) This is obviously impossible unless you know the variables at compile time (and you know the types can't change). In principle, one could set things up so that it's possible to dynamically assign to undefined variables that are referenced inside a function, but it wouldn't work with type inference and it's a lot of work for something that probably isn't applicable outside of JLD. |
This is the flip side of what makes Julia so much faster than Matlab: you rely on the ability to compile fast code. |
@simonster's suggestion at #83 (comment) isn't so ugly. It makes sense if you really need to use variables loaded from a file. One could also add typeasserts to allow the compiler to generate efficient code. Maybe there's a simpler solution: All of these problems are quite similar to those encountered with |
it would be nice if @load read the file when the code was executed, as opposed to when the macro was expanded, so that errors like that below were not an issue.
julia> using HDF5, JLD
julia> isfile("foo.jld")
false
julia> @load "foo.jld"
ERROR: File foo.jld cannot be found
julia> if(isfile("foo.jld"))
println("foo exists")
@load "foo.jld"
end
ERROR: File foo.jld cannot be found
julia> versioninfo()
Julia Version 0.3.0-prerelease+2486
Commit 7211853 (2014-04-04 06:05 UTC)
Platform Info:
System: Darwin (x86_64-apple-darwin13.1.0)
CPU: Intel(R) Core(TM) i7-2640M CPU @ 2.80GHz
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY)
LAPACK: libopenblas
LIBM: libopenlibm
julia> Pkg.status()
2 required packages:
5 additional packages:
The text was updated successfully, but these errors were encountered: