19

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

Sled
  • 18,541
  • 27
  • 119
  • 168
Mark Hula
  • 303
  • 1
  • 2
  • 4
  • 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. – Nicol Bolas Jun 07 '13 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. – Tom Blodget Jun 07 '13 at 14:10

2 Answers2

24

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
Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
  • 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 – Mark Hula Jun 07 '13 at 14:06
  • 1
    Lambda functions used correctly are not messy. It's time you dropped you ancient C idioms and moved on :) – Bartek Banachewicz Jun 07 '13 at 14:08
  • @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. – hugomg Jun 07 '13 at 21:17
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
dualed
  • 10,262
  • 1
  • 26
  • 29
  • 1
    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` – hugomg Jun 07 '13 at 15:33
  • Which is exactly why there is no `local` in the code sample ;) – dualed Jun 07 '13 at 16:29