4

As far as I could understand from the documentation and several posts on the web, the statement

include("myfile.jl")

just takes the code in myfile.jl and pastes it on the calling file (or in the console), replacing the line with the include statement.

Please correct me if I am wrong. I am just beginning with Julia. However, I have also seen the following comment by one of Julia's creators:

"include works in the dynamically-current module, not the lexically-current one.
It is really a load-time function, not a run-time one."

What is the difference between dynamically-current and lexically-current?

Soldalma
  • 4,636
  • 3
  • 25
  • 38

2 Answers2

7

It's not a function call because if myfile.jl is just a = 2 and you do include("myfile.jl"), then you can check in the REPL that a = 2. In functions that a would have been defined in a different scope, and then erased after the function is ended. So this is a clear difference in behavior. Here's an example REPL session demonstrating the difference:

julia> a
ERROR: UndefVarError: a not defined

julia> function incl(file)
           a = "not 2"
           include(file)
           @show Main.a
           @show a
       end
incl (generic function with 1 method)

julia> incl("myfile.jl")
Main.a = 2
a = "not 2"
"not 2"

julia> a
2

This is what they mean by "dynamically-current" vs "lexically-current". Lexically, a function runs in its own scope which is only accessible from within the functions actual code – there is no other way to access or change local variables. include always runs at the current global scope even when called from inside of a function. Julia's eval function behaves similarly – you cannot see or change local variables with eval only global ones.

StefanKarpinski
  • 32,404
  • 10
  • 86
  • 111
Chris Rackauckas
  • 18,645
  • 3
  • 50
  • 81
  • I am still confused as the detailed answer by Josh Watzman in http://stackoverflow.com/questions/22394089/static-lexical-scoping-vs-dynamic-scoping-pseudocode seems to say that "dynamic" refers to run-time (it depends on what is on the stack) while the comment I quoted in my post says include is a load-time function, not a run-time one. Does the include statement replaces itself in the source code before compilation starts (load-time function) or does it wait until execution gets to that line and only then executes the replacement code (run-time function)? – Soldalma Dec 22 '16 at 20:31
  • 2
    I've added an example and edited the answer significantly (hope that's ok, Chris). Hopefully that clarifies the situation a bit. – StefanKarpinski Dec 24 '16 at 16:08
  • The edits are great. I learned too. It makes the naming scheme make sense. To expand for Soldalma (and myself), functions in Julia aren't dynamic since they are compiled. The dynamic scope in Julia is the global scope of a module: this is the scope that doesn't have just a set piece of assembled code (functions in Julia specialize by dispatch, so it may have many different meanings, but all are fixed). So include runs at the current global/dynamic/module scope. I understand that module scopes are dynamic because the REPL has to run in one (`Main`), but Stefan might have a more refined answer. – Chris Rackauckas Dec 24 '16 at 16:17
  • Also then should be noted is the answer to the question: "what if I want to include Julia code into the lexical scope (directly into the function)"? You can do that via macros. [The `@def` macro here](https://discourse.julialang.org/t/def-macro-generator-broken-on-master/1096/3) defines macros for doing exactly this: compiler copy/paste (the example defines `@test` which pastes the code `a=2`, but arbitrary expressions work). Though I use this a bit, this is not idiomatic Julia (and violates some coding principles) so I'll keep it buried in this comment. – Chris Rackauckas Dec 24 '16 at 16:20
0

It means that you will have this code right away in place of include when your application starts. "Include" won't be there in running application. It means that your application may load little bit slower while loading if you use include (it is not noticeable, i mention it for better understanding), but it will run with include replaced by code. Load time is before your application start running.

Kamil Banaszczyk
  • 1,133
  • 1
  • 6
  • 23