Function passed to imp.wakeup by value or by reference

This is partially a question and partially an example i would like to offer. I was trying to build a wakeup function that I could change with other code. I wanted the imp, when woken to perhaps run a different function than originally planned in the wakeup call. I think this technique is a little related to C function pointers which do not exist in this system.

I found that if I referenced a function directly I could not change it. If I wrap the function inside another function then I can change it. My example below is an attempt to show that.

My question is whether anyone thinks this is a bad technique? is there a better one? I am not sure how I would use this; I am just trying to feel out the language.

`

function testdefine(){
server.log(“test define”);
}

function testSecond(){
server.log(“test Second”);
}

function testFinal(){
server.log(“test Final”);
}

function aChangingFunction(){

placeholder();

}

placeholder <- testdefine;//testdefine is a function

placeholder(); //prints "test define"
aChangingFunction(); //prints “test define”

placeholder = testSecond;

server.log (".");
aChangingFunction(); //prints “test Second"
placeholder(); //prints “test Second"
server.log (”.”);

function changeit()
{
placeholder=testFinal;
server.log(“changed”);
}

imp.wakeup(7, placeholder); //will print “test Second”

placeholder=testdefine;

imp.wakeup(37,placeholder);//at the time is testdefine, doesn’t change

imp.wakeup(15,function() {changeit(); imp.wakeup(5,placeholder);});

imp.wakeup(60,aChangingFunction);

server.log(imp.getmemoryfree());
`

output is :

2/19/2013 12:47:00 PM: Device booting
2/19/2013 12:47:00 PM: test define
2/19/2013 12:47:00 PM: test define
2/19/2013 12:47:00 PM: .
2/19/2013 12:47:00 PM: test Second
2/19/2013 12:47:00 PM: test Second
2/19/2013 12:47:00 PM: .
2/19/2013 12:47:00 PM: 56796
2/19/2013 12:47:07 PM: test Second
2/19/2013 12:47:16 PM: changed
2/19/2013 12:47:21 PM: test Final
2/19/2013 12:47:39 PM: test define
2/19/2013 12:48:04 PM: test Final

The second-to-last printing of “test define” is something I thought might be “test Final” because I change what placeholder ‘points’ to.

Thanks for sharing.
Seems to be useable as a kind of void function pointer.
Will need some hands-on to gat a better feel for it, though.

I suspect the reference to the function to call back is taken at the time imp.wakeup is called, which explains the behavior. It’s not the variable, it’s the thing to execute.

Thanks Hugo, I see it now.

Here is a cleaned up example that I think shows how this works.

`
function testFirst(){server.log(“t First”);}

function testSecond(){server.log(“t Second”);}

function testFinal(){server.log(“t Final”);}

placeholder <- testFirst;//testFirst is a function

function aChangingFunction(){
placeholder();
}

placeholder(); //prints "t First"
aChangingFunction(); //prints “t First”

placeholder = testSecond;

aChangingFunction(); //prints "t Second"
placeholder(); //prints “t Second”

function changeit()
{
placeholder=testFinal;
server.log(“changed to testFinal”);
}

placeholder=testFirst;

server.log(".");
placeholder();
server.log("-");

imp.wakeup(5,changeit);
imp.wakeup(10,function() {placeholder();});
imp.wakeup(15,placeholder);
imp.wakeup(20,aChangingFunction);`

prints this:

6:07:59 AM: Device booting
6:07:59 AM: t First
6:07:59 AM: t First
6:07:59 AM: t Second
6:07:59 AM: t Second
6:07:59 AM: .
6:07:59 AM: t First
6:07:59 AM: -
6:08:04 AM: changed to testFinal
6:08:10 AM: t Final
6:08:15 AM: t First
6:08:21 AM: t Final

so in the end, pretty elementary.