Analysis of Package definition learning notes in LUA

Source: Internet
Author: User

LUAAboutPackageThe definition of learning notes is the content to be introduced in this article, mainly to learnLuaModeratePackageFor details, refer to this document.PackageIs a logical concept. That is to say, it is likely that it is not directly provided by the language. The namespace in cpp can be used to implementPackage, InLUAIt relies on the table mechanism. The implementation method is similar. This article summarizes 《LuaThere are three methods in the Chinese manual.

Method 1: directly define the data and methods of all the components in the package. The code looks like this:

 
 
  1. Vector3d ={} -- package name
  2. Function vector3d. function1 ()
  3. ......
  4. End
  5. Function vector3d. function2 ()
  6. ......
  7. If (vector3d. function1 () then
  8. ......
  9. End
  10. End
  11. Return vector3d

This defines a vector3d package. After you open this package in the require language, you can use the vector3d. function1 and vector3d. function2 functions.

This is one of the most straightforward and understandable Package definitions, but it has some drawbacks. This disadvantage is mainly reflected in the Package implementation process. As you can see

If you use the function1 () function in vector3d. function2 (), you must add the complete vector3d package name. Otherwise, you cannot call the function. The author of the Package is a little tired, but the user is fine. Pay special attention to the final return vector3d statement. With this sentence, the caller can rename the package as follows:

 
 
  1. MyPackage =  require "vector3d"  
  2. MyPackage.function2() 

Method 2: Use a local function to define allPackageFunction, and then directly put the function to be exposed at the end of the Package into the Package. The code looks like this:

 
 
  1. Vector3d ={} -- package name
  2. Local function function1 ()
  3. ......
  4. End
  5.  
  6. Local function function2 ()
  7. ......
  8. If (function1 () then
  9. ......
  10. End
  11. End
  12. Vector3d = {function1 = functoin1,
  13. Function2function2 = function2
  14. }
  15. Return vector3d

The final part assigned to the package is to expose the required interfaces. The advantage of doing so: functions that do not need to be disclosed can be completely hidden (all are local functions); the Package name is no longer needed to distinguish between functions called by each other in the Package; you can rename the interface name exposed by the Package as needed. The disadvantage of this method is that you need to write the local code when defining it. This is not a drawback. It depends on you --. Personally, I prefer this definition method. Clearly differentiate between interfaces and private definitions. The name of public interfaces can be changed at will, which means that internal implementations can be replaced without affecting external callers.

No matter what method is used to define a Package, it is to better plan the code hierarchy logically.LUAThe table mechanism in is indeed an infinite mechanism. Package relies on this implementation,LUASome of its own mechanisms also depend on the Table (for example, the global variables are placed in the _ G Table)

How to "Disassemble" in the manual"Package(Package is also the logical product built on the table ). To split a Package, put the public names of all packages in the _ G table. That is, change Package. A () to _ G.A (_ G does not need to be written in general, and is referenced by default)

 
 
  1. function openpackage (ns)  
  2.       for n,v in pairs(ns)   
  3.      do  
  4.             _G[n] = v  
  5.      end  
  6. end 

Summary: AnalysisLUAAboutPackageThe content of the definition learning notes has been introduced. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.