什么是UndefvarError?

0 compiler-errors julia

我正在掌握硕士课程.我有一个程序文件,运行版本为julia-0.3.6.我在Linux中将Julia程序升级到版本0.5.0,但文件没有运行.

f=open("../info.dat","r")
order,nt,nx,ny,nshot,srcy=int(split(readline(f)[1:6])
Run Code Online (Sandbox Code Playgroud)

ERROR: LoadError: UndefVarError: int not defined
Run Code Online (Sandbox Code Playgroud)

问题是什么?

Fen*_*ang 6

int函数已在Julia v0.4中弃用,并在Julia v0.5中删除,因此UndefVarError当您尝试使用它时会发生这种情况.(注意,函数是Julia中的第一类对象,因此绑定到名称就像任何其他变量一样.当使用未绑定的名称时,UndefVarError抛出一个.)在Julia v0.5中编写代码的正确方法是

f = open("../info.dat", "r")
order,nt,nx,ny,nshot,srcy = [parse(Int, x) for x in split(readline(f))]
Run Code Online (Sandbox Code Playgroud)

但是,这段代码不是很好,因为它f之后不会关闭.我会推荐

order, nt, nx, ny, nshot, srcy = open("../info.dat") do f
    [parse(Int, x) for x in split(readline(f))]
end
Run Code Online (Sandbox Code Playgroud)