Chinaunix首页 | 论坛 | 博客
  • 博客访问: 685590
  • 博文数量: 132
  • 博客积分: 10060
  • 博客等级: 上将
  • 技术积分: 1732
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-21 12:35
个人简介

迷惘的码农。

文章分类

全部博文(132)

文章存档

2013年(1)

2011年(2)

2010年(9)

2009年(41)

2008年(79)

我的朋友

分类:

2009-01-03 23:15:29

----------------------- Page 1-----------------------137


           Modules and Packages
           模块和包


    MENU
15.1 The require Function
     函数 require
15.2 The Basic Approach for Writing Modules
     编写模块的基本方法
15.3 Using Environments
     使用环境
15.4 The module Function
     函数 module
15.5 Submodules and Packages
     子模块和包



Usually, Lua does not set policies. Instead, Lua provides mechanisms that are powerful enough for groups of developers to implement the policies that best suit them. However, this approach does not work well for modules. One of the main goals of a module system is to allow different groups to share code. The lack of a common policy impedes this sharing.
通常Lua并不设定规则。代替的做法是,Lua为成组的开发者提供足够有效的机制来实现最适合他们的规则。但是,这种方式不能很好地用于模块。模块系统的一个主要目标是允许不同的组分享代码。公共规则的缺失阻碍了这种分享。

    Starting in version 5.1, Lua defines a set of policies for modules and packages (a package being a collection of modules). These policies do not demand any extra facility from the language; programmers can implement them using what we have seen so far: tables, functions, metatables, and environments. However, two important functions ease the adoption of these policies: require, for using modules, and module, for building modules. Programmers are free to re-implement these functions with different policies. Of course, alternative implementations may lead to programs that cannot use foreign modules and modules that cannot be used by foreign programs.
    从版本5.1开始,Lua为模块和包(包是一个模块集合)定义了一套规则。这些规则不需要任何语言上的额外功能;程序员可以实现他们,只需用我们目前为止看到的东西:表、函数、元表和环境。不过,两个重要的函数让这些规则的采用变得简单:require用于使用模块,module用于建立模块。程序员可用不同的规则自由地重新实现这些函数。当然,变体实现可能导致程序不能使用外部模块以及模块不能被外部程序使用。

    From the user point of view, a module is a library that can be loaded through require and that defines one single global name containing a table. Everything that the module exports, such as functions and constants, it defines inside this table, which works as a namespace. A well-behaved module also arranges for require to return this table.
    从用户的角度看,模块是个包含了表的库,该库能透过require加载并定义了单个全局名字。模块导出的每个东西,比如函数和常量,都定义在表内,该表效果如同命名空间。行为良好的模块也安排require返回该表。

    An obvious benefit of using tables to implement modules is that we can manipulate modules like any other table and use the whole power of Lua to create extra facilities. In most languages, modules are not first-class values (that is, they cannot be stored in variables, passed as arguments to functions, etc.), so those languages need special mechanisms for each extra facility they want to offer for modules. In Lua, you get extra facilities for free.
    用表来实现模块的一个明显的好处是,我们能像任何其他表那样处理模块并能使用Lua的全部能力来创建额外的功能。在多数语言中,模块不是第一类值(即他们呢不能被存储在变量中、作为参数传入函数,等等),所以那些语言对它们想要为模块提供的每个额外功能都需要特殊的机制。在Lua中,你自由地获得额外功能。

    For instance, there are several ways for a user to call a function from a module. The simplest is this:
    例如,用户调用模块中的函数有多种方法。最简单的是这样:


----------------------- Page 2-----------------------138


      require "mod"
      mod.foo()

If she prefers a shorter name for the module, she can set a local name for it:
如果她想要给模块更短的名字,可为它设置局部名字:

      local m = require "mod"
      m.foo()

She can also provide alternative names for individual functions:
她也能给个别函数提供备用名字:

      require "mod"
      local f = mod.foo
      f()

The nice thing about these facilities is that they involve no explicit support from the language. They use what the language already offers.
关于这些功能的好事情是,它们不涉及语言的显式支持。它们用的是语言已经提供的东西。
阅读(1552) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~