Direct calls to lambdas inside a vector


#1

I have one class that just holds values.

//
// Timer class
//
class Timer
{    
    var id
    var callback
    var current
    var last
    var count
    var step
	
    def Timer(id, callback, step, count)
    {
        this.id = id
        this.callback = callback
        this.count = count
        this.step = step
        this.current = 0
        this.last = 0
    }
}

and one variable that holds a vector of this class

var timers = []

I do push elements to this vector using

timers.push_back(Timer(0, def() { print("hello world"); }, 0, 0);

each game iteraction, I do update this timers using the following script, but the problem is that I can’t call the callback directly, like:

for (var i = 0; i < timers.size(); i += 1)
{
    timers[i].callback()
}

no exception is thrown, but the lambda isn’t getting called. I found that if I change the code to this:

for (var i = 0; i < timers.size(); i += 1)
{
    var cb = timers[i].callback
    cb()
}

the script works just fine.

Is this a bug, or what?

Thanks!


#2

Looks like a bug, please open an issue on the github bug tracker https://github.com/ChaiScript/ChaiScript/issues

Thanks


#3

hmm, on second thought, I’m not sure what my options would be there to make that parsing work. Could you try this:

(timers[i].callback)()

And let me know if that works please?


#4

using

(this.timers[i].callback)()

doesn’t seems to solve the problem.


#5

I’ve filled a bug report on github here:

I’ll be watching the issue on github.


#6

The github issue has been closed. Calling functions contained in other objects should work in many more contexts now.