JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.6k stars 5.48k forks source link

Initial require goes beyond .jl file line length #2417

Closed gcamilo closed 11 years ago

gcamilo commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

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

JeffBezanson commented 11 years ago

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.