lua-users home
lua-l archive

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

On 2015-07-05, at 11:59 AM, steve donovan <> wrote:
> On Sun, Jul 5, 2015 at 2:39 PM, Jay Carlson <> wrote:
>> Could I call a function like table.append(c, 4)?
> I think the idea of a tuple is that it is a const unmodifiable table.
>> I had a more complicated proposal for "static" which might help. "static c = Const{1,2,3}" would do the same thing, even inside a loop.
> So it would be a _compile-time_ constant? That makes it cheap to use,
> although restricts what values it can be initialized to

I think the compiler for Lua will remain (relatively) simple. 

The "static" I'm thinking of is a tree transformation such that:

for k,v in f() do
  static s = {1,2,3}

is transformed into

local __t1 = {1,2,3}
for k,v in f() do
  local s = __t1

This lets you use Const() or Set() constructors without invoking them on every loop. 

The transformation is simple, except for any or all of these complications:

1) The compiler may not need to actually introduce a local "s". It seems to me "s" aliases __t1, and the slot number can be reused. The intent is just that the scope of "s" is limited to the body.

2) The rule might be that initializers are "hoisted out of all functions" instead of "hoisted out of any body into top-level chunk"; this kind of irregularity is of the same sort as "local function".

3) A form of "static" syntax is almost exactly what you want for localizing globals. Everybody loves the (premature?) optimization of moving references out of _ENV["name"] into "local name=name", so perhaps "static local name" does just that.

4) In order to deal with re-import of modules, all of the initializers may get bunched up into a single function body

The one nasty problem a simple tree transformation doesn't solve is use of previously-declared locals, especially inside nested chunk bodies. I don't remember whether I solved this the last time I typed this up. I have been avoiding looking up my own message in the archives to see if I can still remember how number 4 worked without a reminder. :-) Perhaps if I can't, it is not a very good idea...


(it still might not be a good idea)