Skip to content

Initial require goes beyond .jl file line length #2417

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

Closed
gcamilo opened this issue Feb 27, 2013 · 5 comments
Closed

Initial require goes beyond .jl file line length #2417

gcamilo opened this issue Feb 27, 2013 · 5 comments

Comments

@gcamilo
Copy link

gcamilo commented Feb 27, 2013

Hi, I'm having a weird problem. I have a program partial.jl (and some auxiliary files) that performs some computations, but if I run julia and run require("partial.jl"), the main file i will run some parts and then return

 julia> require("partial.jl")

 Computing VFI for deterministic income and no mortality risk 
 Elapsed 131.1574420928955
 ERROR: no method start(Nothing,)
  in include_from_node1 at loading.jl:76
  in reload_path at loading.jl:96
  in reload at loading.jl:60
 at /home/gcam/dirkProject/partial.jl:1390

The first two lines are part of the program, but then the rest is the bug. My file only has 195 lines! If I afterwards run

 reload("partial.jl")

It runs perfectly fine. There must be something wrong with the initial require that is giving me trouble. Or there is not a proper EOF in my partial.jl? If you want the code that generated this issue it is at http://gcamilo.com/compartir/dirkProject.zip

@kmsquire
Copy link
Member

I'm curious why you're using reload throughout partial.jl to load code? I'm also unsure of the consequences of reloading or requiring a file in the middle of a function definition--probably not a good idea, and it's likely what's causing the line numbering to be screwed up.

Changing all of the reloads to requires and moving them to the top of each file allows it to run to completion:

julia> require("partial.jl")

     Computing VFI for deterministic income and no mortality risk
     Elapsed 129.54883098602295

     Computing VFI for deterministic income WITH mortality risk
     Elapsed 134.35323786735535

Oh, and you probably want "using Stats" instead of "require("Stats")".

@gcamilo
Copy link
Author

gcamilo commented Feb 27, 2013

I was actively changing the functions I was loading, so this prevented me from closing julia. Changing stuff around like you said does fix it. Thanks!

The issue still remains though.

Why is using better than require?

@JeffBezanson
Copy link
Member

I am looking into this, but for now @kmsquire is right --- it's not recommended to sprinkle reloads and requires all over in random places. For example you reload vfiDeterministic.jl, but that file just defines a function, so it will already be defined and there is no need to reload. It is also not a good idea to use require inside a function. By the time the require happens, it is too late. It also obfuscates the code, since one cannot tell whether a name might refer to something in Stats without knowing whether that particular function has been called yet. You want to just put using Stats at the top.

I also recommend using global variables less, and generally doing less at the top level --- it will make your code easier to test and reuse. The way you wrote it, all one can really do is reload the file and rerun your whole process. By breaking things into definitions, you can interactively test and rerun parts of it without starting over.

require brings code into the system, but using both does a require (if necessary) and makes everything exported by Stats visible in your namespace.

@gcamilo
Copy link
Author

gcamilo commented Feb 27, 2013

I didn't even know I was using global variables, can you tell me which are?

@JeffBezanson
Copy link
Member

When you assign var = val at the top level of a file, not inside any function, that is a global variable.
I will fix the line number. The other error is an artifact of loading Stats too late. Move the requires to the top level and it should be fine.

JeffBezanson added a commit that referenced this issue Feb 28, 2013
staticfloat added a commit that referenced this issue Mar 6, 2021
This should include the recent `is_stdlib()` fixes.  Short commit log:

```
7a9d9654 (HEAD -> master, origin/master, origin/HEAD) [ext/HSG]: Store next release _and_ latest nightly (#2418)
7b870924 [ext/HSG]: Enable generating historical stdlibs on macOS (#2417)
5d496193 Update Project.toml
feada149 only use the stdlib version cache if a custom version is given to the resolver
bae808dc Fix Markdown table formatting (#2416)
6e8b6214 Update docstrings for io kwargs, some io kwarg fixes, update stdlib list (#2402)
c2e3879e Mark the "STDLIBS_BY_VERSION up-to-date" test as broken (#2409)
```
staticfloat added a commit that referenced this issue Mar 6, 2021
This should include the recent `is_stdlib()` fixes.  Short commit log:

```
7a9d9654 (HEAD -> master, origin/master, origin/HEAD) [ext/HSG]: Store next release _and_ latest nightly (#2418)
7b870924 [ext/HSG]: Enable generating historical stdlibs on macOS (#2417)
5d496193 Update Project.toml
feada149 only use the stdlib version cache if a custom version is given to the resolver
bae808dc Fix Markdown table formatting (#2416)
6e8b6214 Update docstrings for io kwargs, some io kwarg fixes, update stdlib list (#2402)
c2e3879e Mark the "STDLIBS_BY_VERSION up-to-date" test as broken (#2409)
```
ElOceanografo pushed a commit to ElOceanografo/julia that referenced this issue May 4, 2021
This should include the recent `is_stdlib()` fixes.  Short commit log:

```
7a9d9654 (HEAD -> master, origin/master, origin/HEAD) [ext/HSG]: Store next release _and_ latest nightly (JuliaLang#2418)
7b870924 [ext/HSG]: Enable generating historical stdlibs on macOS (JuliaLang#2417)
5d496193 Update Project.toml
feada149 only use the stdlib version cache if a custom version is given to the resolver
bae808dc Fix Markdown table formatting (JuliaLang#2416)
6e8b6214 Update docstrings for io kwargs, some io kwarg fixes, update stdlib list (JuliaLang#2402)
c2e3879e Mark the "STDLIBS_BY_VERSION up-to-date" test as broken (JuliaLang#2409)
```
antoine-levitt pushed a commit to antoine-levitt/julia that referenced this issue May 9, 2021
This should include the recent `is_stdlib()` fixes.  Short commit log:

```
7a9d9654 (HEAD -> master, origin/master, origin/HEAD) [ext/HSG]: Store next release _and_ latest nightly (JuliaLang#2418)
7b870924 [ext/HSG]: Enable generating historical stdlibs on macOS (JuliaLang#2417)
5d496193 Update Project.toml
feada149 only use the stdlib version cache if a custom version is given to the resolver
bae808dc Fix Markdown table formatting (JuliaLang#2416)
6e8b6214 Update docstrings for io kwargs, some io kwarg fixes, update stdlib list (JuliaLang#2402)
c2e3879e Mark the "STDLIBS_BY_VERSION up-to-date" test as broken (JuliaLang#2409)
```
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