S.H.W
S.H.W

Reputation: 1

Why we don't use named local function to create "private function" in Lua?

Lua dosen't provide protection level keywords such as public / private / protected. If we want to create a field that only useable in current area, most of the time we use "local" keyword. for example:

-- field cannot be access in other Lua files
local privateVal = 0

-- field can be read or write outside
publicVal = 1

However, I hardly see the defination below:

-- define a named function as local, so that it cannot be use outside
local calc = function(item)
return item.a + item.b
end

-- use it locally
result = calc(myitem)

Why this style hardly use in Lua programming? I think maybe it is difficult to transmit the parameter "self", or there are some performance loss in function creation prograss.

Are there any other more critical reasons?

Upvotes: 0

Views: 97

Answers (1)

shingo
shingo

Reputation: 27341

Why this style hardly use in Lua programming?

On Github, searching for /local [\w_]+? = function/ language:Lua returns 181k files, searching for "local function" language:Lua returns 631k files. I think this style should be quite common.

Upvotes: 3

Related Questions