Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redefining types in Julia: invalid redefinition of constant

Tags:

types

oop

julia

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

like image 438
ccsv Avatar asked Nov 11 '14 22:11

ccsv


1 Answers

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

like image 62
Toivo Henningsson Avatar answered Nov 18 '22 06:11

Toivo Henningsson