[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: In praise of globals
- From: Philipp Janda <siffiejoe@...>
- Date: Thu, 25 Apr 2013 05:29:50 +0200
Am 25.04.2013 01:32 schröbte Jay Carlson:
[...]
===
local a=7
local a
print(a)
===
In this example it doesn't matter whether you paste this at the REPL or not. :-)
It does if you have a non-nil global "a".
[...]
I don't have any suggestions. But writing your code in localized style implies you either won't be *interacting* with it, or that you have a more sophisticated IDE than just an xterm.
My suggestion would be to make a proper interactive interpreter that
respects the local environment. I have some ideas, but it is late, so
maybe they are utter nonsense ...:
Assuming we have a valid chunk that can be compiled via load(string). If
we have identified the locals in the previous chunk using something like
lbci[*] we can do some source code transformation on the current chunk
to add those locals at the beginning. Problem is, we need a way to save
the locals at the end of a chunk, and we need to handle upvalues. I
think we can solve both problems in one go:
We append something like "\n; return function() return a, b, c end",
where `a`, `b`, `c` are the locals, to the end of the chunk, and get a
function which we can query for the upvalue ids.
To load the next chunk `chunk2` we need to run something like
return function( a, b, c )
return function()
<chunk2>
; return function() return a, b, c, d, e end
end
end()
to get a function that is equivalent to `chunk2` but uses upvalues for
the locals `a`, `b`, and `c`. Now we can use debug.upvalueid and
debug.upvaluejoin, to merge the upvalues of the function returned from
the last chunk and this one.
And if that works, we can _finally_ get rid of "global by default" :-p ...
What do you think? Did I miss something?
Jay
Good night,
Philipp
[*]: What is the status of lbci for Lua 5.2 by the way?