: vs .
Closed this issue · 12 comments
To what if any extent is the object:function (versus object.function) syntax supported?
@unhandyandy Do you mean on the Perl side? Could you give an example?
Suppose I have a table
object = { 'y' : 5 }
I'm wondering what happens if I define a function like
function object:action( x )
return self.y + x
end
Will I be able to make a call within the same inline lua section like
object:action( 3 )
and get 8?
Or should I stick strictly to the dot (.) notation?
In Lua, :
and .
for function calling mean something slightly different. object:method(args)
is semantical identical to object.method(object, args)
. Rule of thumb: if you define a function with :
, or if a function has a self
parameter, chances are you should use :
.
Right, but my question is whether this behavior is supported in Inline::Lua. I guess the answer is yes.
Correct =)
OK, maybe this is just my daily confusion, but I've got a function defined like this.
function class:action()
....
I assumed that there were two ways of calling it:
instance:action()
(where instance copies class' table) or
instance.action( instance )
however both of those just return the value instance itself. The call that works correctly is
instance.action()
which I would have thought should not work. Is this a 5.1 thing (I'm more familiar with 5.2)? Maybe the problem is that I'm not using the metatable (since you said it doesn't work with Inline)?
AFAIK, the way .
and :
work hasn't changed since 5.1. If you have something defined as function class:action
, you almost always need to call it as instance:action()
. Could you paste the full example of code you're trying?
What if you define a function by
testfun = function ( self )
local root = math.random( 0, 9 )
return root
end
how then should you call it? Here's a MWE.
package mwe;
use Inline 'Lua';
use Inline (Config => DIRECTORY => "/tmp/");
use Inline 'Lua' => <<'LUA';
testclass = {}
function testclass:generate( )
local chcs
chcs = self:submaker( )
return chcs
end
function testclass:new( )
local instance = {}
instance.generate = testclass.generate
instance.submaker = testfun
return instance
end
testfun = function ( self )
local root = math.random( 0, 9 )
return root
end
function proto()
return testclass:new( )
end
LUA
1;
I would have thought that the call
chcs = self:submaker( )
was correct, but when I call this code from Perl this is what happens.
$test= mwe::proto();
$test->{generate}->($test) ## returns the object itself, i.e.
## $VAR1 = { 'generate' => sub { "DUMMY" },
## 'submaker' => sub { "DUMMY" } };
## when dumped
$test->{submaker}->($test) ## returns a number 0-9
But when I replace that call by one that looks wrong, i.e.
chcs = self.submaker( )
the perl code line 2 correctly returns a number.
Ok, this is really weird; I have no idea why it returns a table in your example. I won't be available for the rest of the weekend, but I'll try to look at it when I have a chance.
No hurry. You've already gone above and beyond in your help for me.
I wonder if it might have to with my testfun not actually using the argument self. Perhaps on the Perl side that results in @_ not being fully consumed, and the self argument that was passed remaining on top of the stack?
@unhandyandy Ok, so I've dug into this...and you've found a bug! I fixed the bug and uploaded Inline::Lua
0.10 to PAUSE. It should work properly now.
Awesome, thanks!