[Home] [Downloads] [Search] [Help/forum]

Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Script to check that you use local variables inside functions

Script to check that you use local variables inside functions

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Posted by Nick Gammon   Australia  (23,019 posts)  [Biography] bio   Forum Administrator
Date Wed 06 Sep 2006 04:34 AM (UTC)

Amended on Wed 06 Sep 2006 07:49 AM (UTC) by Nick Gammon

I found this nifty code example on the Lua mailing list. It was written by Roberto Ierusalimschy, the developer of Lua.

-- strict.lua
-- checks uses of undeclared global variables
-- All global variables must be 'declared' through a regular assignment
-- (even assigning nil will do) in a main chunk before being used
-- anywhere or assigned to inside a function.

local mt = getmetatable(_G)
if mt == nil then
  mt = {}
  setmetatable(_G, mt)

mt.__declared = {}

mt.__newindex = function (t, n, v)
  if not mt.__declared[n] then
    local w = debug.getinfo(2, "S").what
    if w ~= "main" and w ~= "C" then
      error("assign to undeclared variable '"..n.."'", 2)
    mt.__declared[n] = true
  rawset(t, n, v)
mt.__index = function (t, n)
  if not mt.__declared[n] then
    error("variable '"..n.."' is not declared", 2)
  return rawget(t, n)

What this does is use a metatable on the global (_G) table to make sure that setting variables, or reading them, is only done to "declared" variables. That is, variables that are explicitly mentioned on the global script space.

This helps prevent common errors like misspelling variable names in functions, or accidentally assigning to a global variable when a local one would be more sensible.

Let's assume you save that to disk as "strict.lua".

Some example code:

require "strict"

function f ()
 a = 2 --> error: assign to undeclared variable 'a'
 print (b)  --> error:  variable 'b' is not declared
 function g ()  --> error:  assign to undeclared variable 'g'
 end -- g
end -- f

f ()

A sensible strategy in these cases would be to put "local" in front of variables and functions that are really supposed to only exist inside the scope of the local function.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #1 on Wed 06 Sep 2006 05:18 PM (UTC)

Amended on Wed 06 Sep 2006 05:19 PM (UTC) by David Haley

Perl programmers out there will recognize this behavior as being very similar to use strict, or, at least, use strict vars. It's very useful for debugging, because it's easy to make a typo in a variable name and cause mysterious and hard-to-find errors.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum

[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]