YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Strange behavior with delegation
Inpiral
#1 Posted : Monday, May 07, 2012 8:53:18 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/7/2012(UTC)
Posts: 4

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
With this code I get "wrong number of parameters" error:

Code:
local a={}.setdelegate(
{
  function _get(idx)
  {
    return function()
    {
      print(idx);
    }
  }
});

local z=a.b();


With
local z=(a.b)();
all works as expected.
fagiano
#2 Posted : Sunday, May 13, 2012 9:29:35 AM(UTC)
Rank: Advanced Member

Groups: Registered, Administrators
Joined: 6/11/2005(UTC)
Posts: 825

Thanks: 0 times
Was thanked: 36 time(s) in 29 post(s)
try the following

Code:

local a={}.setdelegate(
{
  function _get(idx)
  {
    return function()
    {
      ::print(idx);
    }
  }
});

local z=a.b();


the problem is that with

local z=a.b();

the 'this' passed to the innermost function is 'a' so the _get is invoked again to try to fetch 'print' (if you woudn't have a getter then it would fallback to root automatically).

local z=(a.b)();

that is equivalent to

local temp = a.b;
temp();

in this case the 'this' is the root table, that does have a 'print' slot.

with the :: you tell the compiler, "just fetch this from the root".

I hope this makes sense

ciao
Alberto
Inpiral
#3 Posted : Wednesday, May 16, 2012 12:14:59 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/7/2012(UTC)
Posts: 4

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
I see, thanks for explanation, didn't know that such call changes 'this'
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.084 seconds.