【问题标题】:the difference of include and require in torchtorch 中 include 和 require 的区别
【发布时间】:2016-08-05 14:01:49
【问题描述】:

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

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

【问题讨论】:

    标签: torch


    【解决方案1】:

    Torch 包含简单的 Lua dofile 可以在 Torch source 中看到:

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

    另一方面,Lua require 用于加载模块。另请参阅this 答案。

    大致来说,require 和 dofile 做同样的工作,但有两个重要的 差异。首先, require 在路径中搜索文件;第二, require 控制文件是否已经运行以避免 复制工作。由于这些特性,require 是 Lua 中用于加载库的首选函数。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2011-04-07
        • 2010-09-24
        • 1970-01-01
        • 2011-01-25
        • 2015-02-03
        相关资源
        最近更新 更多