This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Henness's profile picture

[Lua][Question] Storing Functions in a Table

Started by Henness, 10 January 2013 - 08:22 AM
Henness #1
Posted 10 January 2013 - 09:22 AM
I have seen a few programs store functions in a table like this one. I just wondering what are the benefits of doing that. Does it allow you to do something that you would not normally be able to do?
Kingdaro #2
Posted 10 January 2013 - 09:38 AM
It makes function renaming a ton easier. Let's I have a neat group of functions that do nothing, and I went about making my functions the "normal" way.


nothing = {}

function nothing.stuff()

end

function nothing.moreStuff()

end

-- like 12 more functions

But then I decide to make my group actually do something! So I'll just rename it to something and


something = {}

function nothing.stuff()

end

function nothing.moreStuff()

end

-- like 12 more functions THAT I HAVE TO RENAME AAAA

Especially annoying if you don't have a text editor with multi-support select, e.g. CC's editor. D:
ChunLing #3
Posted 10 January 2013 - 11:01 AM
And the actual storing of the functions in a table means that you can call them using variables, which is a bit more difficult if you don't have them in a table.

Like:
funcName = read()
if turtle[funcName] then turtle[funcName]() end
There, now I can use user input to decide which turtle function to call, cause they are all in a table (and indexed by strings).
crazyguymgd #4
Posted 10 January 2013 - 11:23 AM
And the actual storing of the functions in a table means that you can call them using variables, which is a bit more difficult if you don't have them in a table.

Like:
funcName = read()
if turtle[funcName] then turtle[funcName]() end
There, now I can use user input to decide which turtle function to call, cause they are all in a table (and indexed by strings).

Well that is incredibly useful for a project I'm working on :)/>
remiX #5
Posted 10 January 2013 - 11:31 AM
A lot of people use it for menu's with handler to call the selected item.


tab = {
  {text = "Option 1", color = colours.red, handler = function() print("...") end},
  {text = "Option 2", color = colours.yellow, handler = function() print("...") end}
}

etc etc.. menu code...

selected = 2 or whatever
...
tab[selected].handler() -- will call the selected function within the table.
theoriginalbit #6
Posted 10 January 2013 - 12:56 PM
I use them for the method that remiX stated, however I do it a little different… I create the function then store its reference… like this


local function doSomething()
  print( "Did it" )
end

local tTable = {
  { text = "Option", colour = colors.orange, action = doSomething }
}

tTable[ selection ].action()

its the same basic principle, the only difference is my doSomething function isn't limited to only being used by the table, anything else can use it to if i want to to…
remiX #7
Posted 10 January 2013 - 01:01 PM
I use them for the method that remiX stated, however I do it a little different… I create the function then store its reference… like this


local function doSomething()
  print( "Did it" )
end

local tTable = {
  { text = "Option", colour = colors.orange, action = doSomething }
}

tTable[ selection ].action()

its the same basic principle, the only difference is my doSomething function isn't limited to only being used by the table, anything else can use it to if i want to to…

Well, there's no need to create a new function just for one line :P/> I only do it the way i stated if I change/do only 5 or less things, else I create a function for it
theoriginalbit #8
Posted 10 January 2013 - 01:03 PM
Well, there's no need to create a new function just for one line :P/> I only do it the way i stated if I change/do only 5 or less things, else I create a function for it

I never stated your way was wrong or anything, I just stated that I don't always use it that way and provided them with another way… If I know my use case and know its not going to be used later I do it like that, but say for a button that also has a key shortcut. I don't I have it as a function.