Chapter 14_2 global variable Declaration

Source: Internet
Author: User

Global variables in Lua do not need to be declared for use. This is handy for small programs, but a simple clerical error in a large program can cause hard-to-find bugs.

However, this performance can be changed. Because LUA places global variables in a normal table, it is possible to change its behavior when accessing global variables through a meta-table.

One way is to simply detect all access to a nonexistent key in the global table:

setmetatable(_g, {__newindex=function(_, N)Error("attempt to write to undeclared variable".. N2)                    End,--The comma here can't be saved, because it's in the table constructor.__index =function(_, N)Error("attempt to read undeclared variable".. N2)                    End,--The comma here can't be saved, because it's in the table constructor.})

After executing this code, all key accesses that do not exist in the global table will raise an error:

Print (a)        -- > Stdin:1: Attempt to read undeclared variable a

But how do you declare a new variable?

One approach is to use Rawset to bypass the meta-table:

function declare (name, initval)     Rawset (_gorfalse)    -- false here to make sure the new variable is not nil End

Another simpler approach is to allow the assignment of global variables only in the main block , when the following variables are declared:

A  =  1

Just check to see if this assignment is in the main program block. This can be used with the debug library, which calls Debug.getinfo (2, "S") to return a table,

Where is the field that indicates whether the function calling the meta-method is the main block or the normal LUA function, or the C function.

The __newindex meta-method can be overridden by this function:

__newindex =function(t,n,v)LocalW =Debug.getinfo(2,"S"). WhatifW ~="Main"  andW ~="C"  Then  --receive modification assignments for main and C code        Error("attempt to write to undeclared variable".. N2)    End    Rawset(t,n,v)End

This version can receive the C code assignment, because the general C code knows what they are doing.

To test whether a variable exists, you cannot simply compare it to nil. Because if it is nil, access throws an error, and you can also use Rawget to bypass the meta-method:

if Rawget (_g , var) = =Nilthen    --  ' var ' is not declared      ... End

As mentioned earlier, global variables are not allowed to have nil values because global variables with nil are automatically considered undeclared.

However, it is not difficult to correct this problem by simply introducing an auxiliary table to hold the name of the variable that has been declared.

Once the meta method is called, examine the table to determine if the variable has been declared:

LocalDeclarednames = {}setmetatable(_g, {__newindex=function(t, N, v)if  notDeclarednames[n] Then                            LocalW =Debug.getinfo(2,"S"). WhatifW ~="Main"  andW ~="C"  Then                                Error("attempt to write to undeclared variable".. N2 )                            EndDeclarednames[n]=true                        End                            Rawset(t, N, v)--complete the actual setup                    End, __index=function(_, N)if  notDeclarednames[n] Then                        Error("attempt to read undeclared variable".. N2 )                    Else                        return Nil                    End                End ,            })

At this point, even the following assignment can act as a declaration of global variables:

Nil

The overhead of the above two methods is negligible;

In the first method, there is no call involving a meta-method at all.

The second method is to call the Meta method only if the program accesses a variable that is nil.

The above: The second edition of LUA programming and the programming in Lua Third edition

Chapter 14_2 global variable Declaration

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.