lua-users home
lua-l archive

Lua 5.2: Can you explain this behaviour?

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


I downloaded a macosx binary of Lua 5.2 from the main website last week.

If you run the code snippet, it'll print "=a<a<". I'd have thought it would be "=a<<.>a<<.>>".

If you toggle line 4 off, though, it will print "=a<<.>a<<.>>" as expected.

I don't understand why, can anyone explain? See the following two code samples.

local function test(bool)
local w, t
if bool
--or true --- toggle this line
then
t = "a"
w = function (s) t = t .. tostring(s) end
_ENV.w = w
else
t = "."
end
local function r()
if bool then
print("r", test(false))
end
return t
end
_ENV.r = r
loadstring("w('<') w(r()) w('>')")()
return t
end

print("=", test(true))

prints:
r.
=a<<.>a<<.>>

local function test(bool)
local w, t
if bool
or true --- toggle this line
then
t = "a"
w = function (s) t = t .. tostring(s) end
_ENV.w = w
else
t = "."
end
local function r()
if bool then
print("r", test(false))
end
return t
end
_ENV.r = r
loadstring("w('<') w(r()) w('>')")()
return t
end

print("=", test(true))

prints:
ra<a<>
=a<a<









AltStyle によって変換されたページ (->オリジナル) /