local function A()
B()
end
你写的 local 的会出错,还不会 stack overflow,如果是全局的就会出现 stack overflow 了.
2010/3/2 John <ich...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups "Lua中国用户组" group.
> To post to this group, send email to lua...@googlegroups.com.
> To unsubscribe from this group, send email to lua_cn+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/lua_cn?hl=en.
>
>
我不是防止这个循环引用,而是需要用这个特性。但是发现local的function不能,而global的function却可以(当然上面的代码不
加条件break的话会stack overflow),想知道其中的原委。
对于 local 会出错,我的看法是 local 变量的作用域是在它声明之后的,所以在你的代码里
local function B()
A()
end
这里的 A 会是 nil,因为 local A 的声明在后面.
如果翻译成c应该会更清楚,局部和全局的操作是不一样的,全局的用的是
lua_setglobal/lua_getglobal,而局部用的应该是栈里面的变量操作(或者其他操作,我不是很确定)
2010/3/2 John <ich...@gmail.com>:
local function B()
_G["A"]()
end
local function A()
B()
end
如果要求调用的都是local 的函数,应该这样写
local A
On Mar 3, 9:19 am, VirusCamp <virusc...@gmail.com> wrote:
> 如果要求调用的都是local 的函数,应该这样写
>
> local A
>
> local function B()
> A()
> end
>
> local function A()
> B()
> end
哦,这个先声明的我倒是没想起来.
现在想想 John 要的可能是用在某些情况下的循环递归吧
--
You received this message because you are subscribed to the Google Groups "Lua中国用户组" group.
To post to this group, send email to lua...@googlegroups.com.
To unsubscribe from this group, send email to lua_cn+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/lua_cn?hl=en.
local function end()
-- do sth
start()
end
start()
我知道start和end定义为global function的话,会被放在一个全局的表(_G)里,lua会自己去查找,不像C++那样调用之前需
要看到定义。
可是对local function的机制就不甚了解了,下面这种方法(forward declaration)在lua里不能通过:
local A
local function B()
A()
end
local function A()
B()
end
A() -- error: attempt to call upvalue 'A' (a nil value)
在B里调用A时,A的值还是nil,而不是function。
不过A怎么成了upvalue了呢?
> 现在想想 John 要的可能是用在某些情况下的循环递归吧
local function B()
A()
end
A=function()
B()
end
A()
> > 现在想想 John 要的可能是用在某些情况下的循环递归吧- 隐藏被引用文字 -
>
> - 显示引用的文字 -
On Mar 3, 2:04 pm, VirusCamp <virusc...@gmail.com> wrote:
> --额,我写错了,应该这样写,我原来那样是重复定义了A
> local A
>
> local function B()
> A()
> end
>
> A=function()
> B()
> end
>
> A()
>
> On 3月3日, 下午1时33分, John <ich...@gmail.com> wrote:
>
>
这个方法不错~
怎么当时就没有想到lua可以这样呢,看来还得多多熟悉......
多谢指教了!
local A, B
function A()
B()
end
function B()
A()
end
的确是多看一些书,少提一个问题。