Passed function arguments are converted to tables

Probably the easiest question presented here in a long time, but i’ve googled quite a bit, and not one mention of my problem. This means it’s definately me screwing things up. Always a comforting thought! :slight_smile:

Anyway, i just started developing for corona, and in lua. I like it thus far (i’ve got an extensive background in PHP and JS coding), but hit a snag that i just don’t understand.

I’ve got a main.lua wich requires a module i’ve made and required.
The module consists of a single function that i intend to use often (so that’s why it’s in an external module, to keep things tidy).

The bare essential of my problem is (not actual code):


– main.lua

local f = require “functions”

f(‘please write this’)

– functions.lua

module(…, package.seeall)

function test(id)
print(id)
end
As to my (admitted, limited) knowledge this is supposed to just print “please write this” to the debug screen. Instead, it prints table#.

What gives? Why is my string converted to a table? Every tutorial or lua function explanation tells me this should just work…

So, please, have a good laugh, an provide me with the answer :slight_smile: [import]uid: 53467 topic_id: 28575 reply_id: 328575[/import]

The module/require system is intended for you to make a family of functions, not just one function. All these functions are then returned inside a table. For example, the Lua math library (which we require for you) defines a whole bunch of functions such as sin, cos, tan, log, pow, random, sqrt.

So when you do:
local math = require “math”

All the functions are put inside a table which we called math. So you are expected to do things like:
math.sin(0)
math.random()

Not:
math()

To make things a little clearer, ‘math’ is just the local variable name I chose in this case which is used for consistency and clarity. However, in this case, it is just a local variable. I could have called it ‘f’.

local f = require “math”
f.sin(0)
f.random()

This doesn’t work:
f()

[import]uid: 7563 topic_id: 28575 reply_id: 115422[/import]

Ah, i see that i asked my question wrong. My example code was wrong.

It should be as followed:

– main.lua

local f = require “functions”

f:test(‘please write this’)
The example in the first post wouldn’t work in any language i know :slight_smile:

Any ideas?
[import]uid: 53467 topic_id: 28575 reply_id: 115497[/import]

Just to be clear (and giving this thread a shameless bump), this is the corrected code in question:

main.lua

----------------  
-- main.lua  
----------------  
  
local f = require "functions"  
  
f:test('please write this')  
  

functions.lua

-----------------  
-- functions.lua  
-----------------  
module(..., package.seeall)  
  
function test(id)  
print(id)  
end  
  

Anyone any idea why f:test(‘please write this’) writes out “table#ID” out to the logging screen? [import]uid: 53467 topic_id: 28575 reply_id: 115780[/import]

Because you are using the colon syntax.

f:test(‘please write this’) is syntactic sugar for:
f.test(f, ‘please write this’)

You are printing the first argument which is ‘f’ which is a table.
[import]uid: 7563 topic_id: 28575 reply_id: 115824[/import]

Aaah, that’s what i was missing!

Although i do not completely understand why the first (silent) argument would be the “class” itself, printing the second argument gives me the given value.

I suppose the silent argument is to give the function a reference to the class where certain values are set, mimicking this or self properties other languages have.

Thanks alot for your answer! [import]uid: 53467 topic_id: 28575 reply_id: 115829[/import]

Yes, the colon syntax is applying the ‘this’/‘self’ concepts.
Refer to Chapter 5 of Programming in Lua at http://www.lua.org/pil/5.html and its link to Chapter 16.

In your case, you don’t need objects and the colon syntax. Just do:
f.test(‘please write this’)
[import]uid: 7563 topic_id: 28575 reply_id: 115830[/import]

Yeah, figured as much. Thanks for the info and the link. Will continue to read some more about lua coding.

Based on your second to last comment, i altered the code from : to . and worked like a charm.

This topic can be closed.

Thnx! [import]uid: 53467 topic_id: 28575 reply_id: 115831[/import]