在Julia Lang中包含文件的正确方式,以便考虑更新

在Julia Lang中包含文件的正确方式,以便考虑更新,julia,Julia,我承认对朱莉娅来说是新来的。然而,通过查阅各种文件,我找不到我的问题(可能很简单)的适当答案 我从Matlab知道的 考虑文件夹src/中名为main.m和anotherfunc.m function main anotherfunc(0) end 及 我将在命令窗口中运行main,并查看所需的结果(=0)。现在,也许我会改变主意,选择 function otherfunc(x) disp(cos(x)) end 我再次运行main并查看1 关于朱莉娅,我不知道什么 如何做完全相同的事情

我承认对朱莉娅来说是新来的。然而,通过查阅各种文件,我找不到我的问题(可能很简单)的适当答案

我从Matlab知道的

考虑文件夹
src/
中名为
main.m
anotherfunc.m

function main
 anotherfunc(0)
end

我将在命令窗口中运行
main
,并查看所需的结果(
=0
)。现在,也许我会改变主意,选择

function otherfunc(x)
 disp(cos(x))
end
我再次运行
main
并查看
1

关于朱莉娅,我不知道什么 如何做完全相同的事情。 我尝试了两种我认为有效的方法

(一)

这些文件是另一个func.jl:

function anotherfunc(x)
 print(sin(x))
end
function main()
 anotherfunc(0)
end
和(在同一目录中)
main.jl

function anotherfunc(x)
 print(sin(x))
end
function main()
 anotherfunc(0)
end
现在我开始在终端中编写
julia

julia> include("anotherfunc.jl")
anotherfunc (generic function with 1 method)

julia> include("main.jl")
main (generic function with 1 method)

julia> main()
0.0
  function main
     include("anotherfunc.jl")
     anotherfunc(0)
    end
好。现在我将
sin
更改为
cos
并获得

julia> main()
0.0
这并不奇怪,我知道我需要另一个
包括
,即

julia> include("anotherfunc.jl")
anotherfunc (generic function with 1 method)

julia> main()
1.0
因此,这是可行的,但似乎很容易出错,我将在将来忘记包含

(二) 我想我会很聪明,会写作

julia> include("anotherfunc.jl")
anotherfunc (generic function with 1 method)

julia> include("main.jl")
main (generic function with 1 method)

julia> main()
0.0
  function main
     include("anotherfunc.jl")
     anotherfunc(0)
    end
但是关闭julia并再次启动它会

julia> main()
ERROR: MethodError: no method matching anotherfunc(::Int64)
The applicable method may be too new: running in world age 21834, while current world is 21835.
Closest candidates are:
  anotherfunc(::Any) at /some/path/anotherfunc.jl:2 (method too new to be called from this world context.)
Stacktrace:
 [1] main() at /some/path/main.jl:4
这显然是错误的


总结:我不知道处理在开发过程中拆分为多个文件和更改的代码的最佳过程。

最简单的方法是使用而不是
包括
和包

通过调用“Pkg.add”(“Revise”)安装
Revise.jl

我们在您的工作目录或其他目录中的
MyModule.jl
中有以下
Module

module MyModule

export anotherfunc

function anotherfunc(x)
    display(sin(x))
end

end
首先,确保存储模块的目录位于
LOAD\u路径中。默认情况下,Julia的工作目录不会添加到
LOAD\u路径
,因此如果您将模块放入工作目录,请调用
push!(加载路径,pwd())
否则调用
push!(加载路径,“/PATH/to/your/module”)
。您可以将此代码添加到
.juliarc
文件中,以避免对运行的每个
julia
实例调用此代码

现在我们有了下面的主文件

using Revise # must come before your module is loaded.
using MyModule

anotherfunc(0)
现在更改文件
MyModule.jl
,以便
anotherfunc
使用
cos
而不是
sin
并查看结果


我建议您阅读并

FYI,谢谢您提供的有用答案!如果
anotherfunc
相当长,并且我不想将代码放入
MyModule
本身,我该怎么办?也许我想把另一个Func2或另一个Func3也放在那里,这样会变得很乱,不是吗?或者我可以把
include(“…”)
放在模块中吗?是的,你可以这样做。即使您更改了模块中包含的文件,Review也会检测到更改。当检测到模块(或模块中包含的文件)发生更改时,您将看到一条警告。