torch中include和require的区别

the difference of include and require in torch

在 torch(lua) 中 require 和 include 有什么区别?当我们调用 include 或 require 时它在后面做了什么? 例如:

 include('util/test.lua')
 require('util/test.lua')

in require 如果编译器找不到特定的文件,编译器将停止编译其他部分的代码

但是在include中如果编译器找不到文件,开始编译其他部分的代码没有错误

火炬包括只是 Lua dofile as can be seen in the Torch source:

function torch.include(package, file)
   dofile(torch.packageLuaPath(package) .. '/' .. file)
end

另一方面,Luarequire is used to load modules. Also see 回答。

Roughly, require does the same job as dofile, but with two important differences. First, require searches for the file in a path; second, require controls whether a file has already been run to avoid duplicating the work. Because of these features, require is the preferred function in Lua for loading libraries.