[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Strict 'struct' pattern
- From: David Manura <dm.lua@...>
- Date: Wed, 12 Aug 2009 00:54:26 -0400
On Fri, Aug 7, 2009 at 7:59 AM, steve donovan wrote:
> We all know that global variables can be a pain  and should be
True. Without the methods in , I think programming in Lua is error prone.
> It would be possible (using a suitable proxy table) to enforce dynamic
> type checking on field assignments, but of course this would incur a
> run-time cost.
There's also ways to do such things statically. My concept of how
this might work is given in luanalyze :
--! typematch('idx$', 'luaanalyze.type.number')
for my_idx=1,10 do
local s = string
local f = s.format
--print(f(my_idx)) -- fails: got number expected string
--print(myy_idx) -- fails: undefined global
print(myy_idx) -- no error
The special "--!" comments are Lua statements to be executed by the
type checker within the context of the current lexical scope.
typeimport('luaanalyze.library.standard') loads predefined types for
the standard library (e.g. string). typematch associates types to
variables whose name matches some pattern (thereby supporting
Hungarian like conventions). checkglobals() check the AST to ensure
no unbound globals are used. checktypes() evaluates the AST in a
special manner, propagating types, doing basically a very rudimentary
form of dataflow analysis that is extensible with metamethods. The
dataflow analysis could be much improved by someone with more
experience in this area and willingness to devote the time.
A real example of its use is in the source code of lua2c , where,
for example, it checks that invalid fields aren't accessed from AST
 http://lua-users.org/wiki/LuaFish (bottom of page)