16
votes

Lets say I assign a type Person in Julia:

type Person
    name::String
    male::Bool
    age::Float64
    children::Int
end

function describe(p::Person)
    println("Name: ", p.name, " Male: ", p.male)
    println("Age: ", p.age, " Children: ", p.children)
end


ted = Person("Ted",1,55,0)

describe(ted)

Which will output with the function:

Name: Ted Male: true
Age: 55.0 Children: 0

Then I modify the features for type Person where I added a new category to the type eyes

type Person
    name::String
    male::Bool
    age::Float64
    children::Int
    eyes::String
end


ted = Person("Ted",1,55,0,brown)

If I run the function now I get an error

Error evaluating REPL:
invalid redefinition of constant Person
 in include_string at loading.jl:97

What is the best way to work around this when developing new code? other than making a module as suggested in the julia FAQ

1
Why don't you want to make a module? Is the workspace() function to clear the Main module more in line with what you are looking for?Toivo Henningsson
@ToivoHenningsson Yes that is what I am looking for. You have to add it above your script. You can put it in the answer and I will check it off. Thanks!ccsv
This is especially frustrating for editing code in JuliaBox, where rerunning a cell that defines a Type results in an error... :/NHDaly
This seems to be in flux (e.g., see github.com/JuliaLang/julia/pull/25207). Revise.jl (github.com/timholy/Revise.jl) seems to be the current flavor of the day for handling this.dat
Fixing the julia compiler is actually the best answer to "What is the best way to work around this when developing new code?"Geoffrey Anderson

1 Answers

8
votes

You can use the workspace() function to clear the Main module if you don't want to put the code in its own module.