lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It will be great for accessing table and object members!

  global month, day, year in date'*t'

It will be great for named parameters!

  function IO_Socket_INET:new( params )
    global Proto, PeerAddr, PeerPort in params
    assert( Proto, "need protocal" )
    assert( PeerAddr, "need address" )
    -- port is optional
    return self:some_lower_level_function( Proto, PeerAddr, PeerPort )
  end

  sock = IO_Socket_INET:new{ PeerAddr ='www.perl.org',
PeerPort='http(80)', Proto ='tcp' }

But for this usage, I would prefer that Lua use the keyword 'local' or
'bind' or 'import' rather than 'global'.

I never liked Pascal's 'with' because it would pollute your local scope
with everything in a record.  I really like 'global a,b,c in some_table'
because it's selective, and it will save me from typing table.this,
self.that, over and over.


As for 'global in some_table or nil' and the problem of pre-declaring
everything, one should never have to declare any of the functions
registered by the C API.  By this I mean everything in lbaselib.c should
be declared by default.  After all, these would be pseudo-keywords in
other languages.  I guess the problem is that there is no way of knowing
who put the function in the globals() table -- was the C API or some Lua
script?

What if all C functions are declared by default?  ie. functions for
which lua_iscfunction is true.

I also think that functions defined in a scope should be automatically
declared for the rest of the scope.

  global square  --  don't want to have to do this
  function square(x) return x*x end

I guess this is part of the reason for 'global in T' acting like
'namespace T' or 'package T'.


If a Perl script has:

  use CGI; 

What happens is the "compiler" stops "compiling" at the 'use' statement,
opens CGI.pm from the search path, "compiles" it, and evaluates
'CGI->import();'.  Most Perl modules have a 'require Exporter;'
statement that provides a default 'import' method which copies certain
specified variables into the users symbol table so that they are
available when the rest of the users file is "compiled".

The 'use' statement above is the short form of:

  BEGIN { require 'CGI.pm'; CGI->import(); }

Where Perl's require is very much like Lua's require.  The important
point is that Lua doesn't have any form of BEGIN to stop "compiling",
evaluate some code, and continue "compiling", and so there is no way to
fix up the symbol table (add some declarations) before the rest of the
main file is "compiled".

The new global keyword will let you add specific names to the symbol
table, but it wont verify that those names exist in the module being
used.  There might be some ways to work around this though.

I agree with Tom, lets give it a try and see what it can do.

- Peter