2013-08-29 25 views
0

如何根據條件在Lua中將整個函數命名爲false? 例如:Lua宣佈函數爲false

if homeruns == 1 or homeruns == 2 
then function BabeRuth() = false 
end 
+3

你所說的「一個全功能的假指什麼「? –

+0

偶然,你來自[Functional](http://en.wikipedia.org/wiki/Functional_programming)背景嗎? – Textmode

回答

1

我相信你想要的東西是這樣的:

function_name = nil 

你不想因爲這調用功能function_name()

但我不是100%確定我知道你在做什麼。你是否試圖刪除一個函數(比如在沙盒中的安全性)?

0

如果要重新定義功能BabeRuth在調用時返回false(從此以後)條件滿足後,可以將其綁定到一個新的匿名函數,像這樣:

> homeruns = 1 
> if homeruns == 1 then BabeRuth = function() return false end end 
> BabeRuth() 
false 

主要部分是

BabeRuth = function() return false end

5

我猜在這裏,但也許你想這樣的:

function BabeRuth() 
    if homeruns == 1 or homeruns == 2 then 
    return false 
    else 
    return true 
    end 
end 

可以寫得更簡潔的

function BabeRuth() 
    return not (homeruns == 1 or homeruns == 2) 
end 

繼續我的猜謎遊戲,也許你想這個,而不是依賴於全局變量:

function BabeRuth(homeruns) 
    return not (homeruns == 1 or homeruns == 2) 
end