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.