Closures

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Closures

Jack Christensen
I'm getting some results I was not expecting with this code.

functionList = {}

for i=1, 10 do
   functionList[ i ] = function()
print( i ) end
end

for i=1, 10 do
   functionList[ i ]()
end

I get ten 10's printed out. As I understand closures I would have expected 1 to 10. Could someone enlighten me as to what is wrong?

Thanks.

Jack

Reply | Threaded
Open this post in threaded view
|

Re: Closures

RLak-2

Jack Christensen preguntó:

> I'm getting some results I was not expecting with this code.

> functionList = {}
> for i=1, 10 do
>   functionList[ i ] = function()

>      print( i )  
>   end
> end

> for i=1, 10 do
>   functionList[ i ]()
> end

> I get ten 10's printed out. As I understand closures I would have
> expected 1 to 10. Could someone enlighten me as to what is wrong?

I agree. Fortunately, this odd behaviour is slated to change
in Lua 5.1. (see below)

There is nothing wrong with your understanding of closures. It is
the implementation of the "for" loop which you have gotten wrong;
in effect, the loop is:

  local i = 1
  while i < 10 do
    ...
    i = i + 1
  end

As opposed to:

  local _control_ = 1
  while _control_ < 10 do
    local i = _control_
    ...
    _control_ = _control_ + 1
  end

------> results from Lua 5.1 ----->

Lua 5.1 (work)  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
> functionList = {}
> for i = 1, 10 do
>>   functionList[i] = function() print(i) end    
>> end
> for i = 1, 10 do functionList[i]() end
1
2
3
4
5
6
7
8
9
10
>

Reply | Threaded
Open this post in threaded view
|

Re: Closures

Edgar Toernig
In reply to this post by Jack Christensen
Jack Christensen wrote:
>
> I'm getting some results I was not expecting with this code.
> 
> functionList = {}
> 
> for i=1, 10 do
>     functionList[ i ] = function()
>         print( i )   
>     end
> end
> 
> for i=1, 10 do
>     functionList[ i ]()
> end
> 
> I get ten 10's printed out. As I understand closures I would have 
> expected 1 to 10.

Why 1 to 10?  You've created ten closures, right.  But alll reference
the same i.  What's the value of i at the time you call the closure?

You get your expected behaviour (freeze the value at closure creation
time) with this:

for i=1, 10 do
    local i=i  -- for each closure a private 'i'
    functionList[ i ] = function()
        print( i )   
    end
end

Ciao, ET.

Reply | Threaded
Open this post in threaded view
|

RE: Closures

Bilyk, Alex
In reply to this post by Jack Christensen
Would this change from Lua 5.1 apply to "for k,v in <function> do end" and other variations of 'for' as well or just to the version with numeric limits?
Thanks,
Alex 
-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
Sent: Wednesday, May 12, 2004 5:16 PM
To: Lua list
Subject: Re: Closures


Jack Christensen preguntó:

> I'm getting some results I was not expecting with this code.

> functionList = {}
> for i=1, 10 do
>   functionList[ i ] = function()

>      print( i )  
>   end
> end

> for i=1, 10 do
>   functionList[ i ]()
> end

> I get ten 10's printed out. As I understand closures I would have
> expected 1 to 10. Could someone enlighten me as to what is wrong?

I agree. Fortunately, this odd behaviour is slated to change
in Lua 5.1. (see below)

There is nothing wrong with your understanding of closures. It is
the implementation of the "for" loop which you have gotten wrong;
in effect, the loop is:

  local i = 1
  while i < 10 do
    ...
    i = i + 1
  end

As opposed to:

  local _control_ = 1
  while _control_ < 10 do
    local i = _control_
    ...
    _control_ = _control_ + 1
  end

------> results from Lua 5.1 ----->

Lua 5.1 (work)  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
> functionList = {}
> for i = 1, 10 do
>>   functionList[i] = function() print(i) end    
>> end
> for i = 1, 10 do functionList[i]() end
1
2
3
4
5
6
7
8
9
10
>

Reply | Threaded
Open this post in threaded view
|

RE: Closures

RLak-2

> Would this change from Lua 5.1 apply to "for k,v in <function> do end" and other variations of 'for' as well or just to the version with numeric limits?

Both forms of 'for':

Lua 5.1 (work)  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
> f = {}
> for k, v in _G do table.insert(f, function() return k.." is a "..type(v) end) end
> table.sort(f, function(a, b) return a() < b() end)
> for _, fn in ipairs(f) do print(fn()) end
_G is a table
_LOADED is a table
_TRACEBACK is a function
_VERSION is a string
__pow is a function
assert is a function
collectgarbage is a function
coroutine is a table
debug is a table
...

--------------------------
(current version:)
Lua 5.0.2  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
> f = {}
> for k, v in _G do table.insert(f, function() return k.." is a "..type(v) end) end
> table.sort(f, function(a, b) return a() < b() end)
stdin:1: attempt to concatenate a nil value
stack traceback:
        stdin:1: in function `a'
        stdin:1: in function <stdin:1>
        [C]: in function `sort'
        stdin:1: in main chunk
        [C]: ?
>
Reply | Threaded
Open this post in threaded view
|

Luna with properties?

jose marin2
In reply to this post by Edgar Toernig
Hello.

I've found Luna is as easy way of bind Lua/C++
classes, but it only binds methods, not properties.

Is there a new version that does this?

How the professionals do this in a real game?

I mean, how they get the values of Lua objects to use
in C++ code?



______________________________________________________________________

Yahoo! Messenger - Fale com seus amigos online. Instale agora! 
http://br.download.yahoo.com/messenger/