使用 closure 替代 table
前几天谈到 lua 的一些技巧,我整理在 wiki 上了。今天又加了一个,关于 point 结构的封装的。
function point (x,y)
    return function () return x,y end
end
可以用 point(1,2) 构造一个 point 。它比 {1,2} 轻量。
还可以有一个更复杂一点的实现:
function point (x,y)
    return function (idx) 
        if idx=="x" then return x
        elseif idx=="y" then return y
        else return x,y end
    end
end
这个技术在 Programming in Lua 中有介绍。
Comments
Posted by: Cloud![[TypeKey Profile Page]](https://blog.codingnow.com/nav-commenters.gif) | (6) 
                                 November 27, 2007 01:20 PM
 | (6) 
                                 November 27, 2007 01:20 PM 
                              
Posted by: Atry | (5) November 27, 2007 11:57 AM
Posted by: soloist | (4) March 20, 2006 11:58 AM
Posted by: 刘未鹏 | (3) March 18, 2006 02:30 AM
Posted by: Cloud![[TypeKey Profile Page]](https://blog.codingnow.com/nav-commenters.gif) | (2) 
                                 March 16, 2006 06:39 PM
 | (2) 
                                 March 16, 2006 06:39 PM 
                              
Posted by: Atry | (1) March 16, 2006 02:10 PM