Embedded Scripting mit Lua

Page 22

EMBEDDED SCRIPTING MIT LUA

Im globalen Raum darf die Variable value auch nicht liegen, sonst würden sich die unterschiedlichen Zähler gegenseitig beeinflussen: value = 0

function create_counter() return function()

-- all counter access global value value = value + 1

end

end

return value

c1 = create_counter() c2 = create_counter()

print(c1()) -- value == 1 print(c2()) -- value == 2 print(c1()) -- value == 3 print(c2()) -- value == 4

Dies entspricht nicht dem erwarteten Verhalten der (unabhängigen) Zähler.

22

COPYRIGHT © 2015, BBV SOFTWARE SERVICES AG


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.