20

Bit confused by all this; so here's what I am attempting to do! Have a def thus:

block_basic_DEF =
{
    image = "button.png",
    name = "basic block",
    obj_table = this_obj_table.common_objects_table,
    startup = function() init(), <----- This is the problem
}

In another file I access as one would expect with:

function spawn(params)
    local obj = display.newImage(params.image)
    -- etc.

In that block_basic_DEF I wish to pass the address of the init() function such that in my spawn I can do something like:

params.startup() --i.e. actually call the original init function

2
  • 2
    "Have a def thus" That's not a "def". Lua doesn't have "definitions". That's a table, which is a value. Just like functions. Commented Jun 7, 2013 at 13:08
  • params.startup() actually treats the value referenced by params as a table and indexes it with the string startup to get its value (dot operator). Then it invokes the value as a function (parentheses operator). That function then invokes the value referenced by init as a function. Commented Jun 7, 2013 at 14:10

2 Answers 2

26

Lua functions are just values, and you can asssign them using their name without parens:

function init() 
     print("init");
end

block = { 
     startup = init
}

And then call it like a regular function

block.startup()

It is near to OOP, but in fact it's as simple as the fact that a function is a normal value.

If you wanted something more similar to a lambda, you have to spell out the whole function, omitting the name:

startup = function() print("init") end
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks!. The problem was my init() function was defined after my reference to it. I prefer definitions and data at the top of my file not through-out it (always looks messy too me). How can I forward reference something? Cheers
Lambda functions used correctly are not messy. It's time you dropped you ancient C idioms and moved on :)
@MarkHula: For forward referencing you need to declare the variables you are going to use at the top of the file. If its local variables you can declare them with a local var1, var2, var3 line. If declaring too many names is ugly, you can declare a single "namespace" table local M = {} and then use fields as the variables: M.var1 = .... Finally, global variables also work like this except that the namespace table is implicit.
7

You just forgot the end keyword. It is part of a function definition and you can not leave it out. You wouldn't leave out the closing } in C either right?

block_basic_DEF =
{
    image = "button.png",
    name = "basic block",
    obj_table = this_obj_table.common_objects_table,
    startup = function() init() end, -- <-- This was the problem
}

Apart form that, the following two syntax variations are equal:

function foo()
end

foo = function()
end

2 Comments

However, the syntax is not 100% equal if you are declaring a local function. To be able to call the function recursively you need to declare the variable before the assignment: local foo; foo = function() end
Which is exactly why there is no local in the code sample ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.