在Julia中重新定义类型:对常量的重新定义无效

时间:2014-11-11 22:46:54

标签: oop types julia

假设我在Julia中指定了一个Person类型:

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)

将使用以下函数输出:

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

然后我修改了Person类型的功能,我在其中添加了一个新类别eyes

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


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

如果我现在运行该功能,我会收到错误

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

在开发新代码时,解决此问题的最佳方法是什么?除了按julia FAQ

中的建议制作模块

1 个答案:

答案 0 :(得分:10)

如果您不想将代码放在自己的模块中,可以使用workspace()功能清除Main模块。