I’ve been successfully using .call and .acall to invoke methodsfor tables/instances/classes but am having trouble with the methods for default objects like strings. I’ve been trying to pass the object itself as the context. I get an error when that happens. Is there an alternate syntax that I can use? Using acall() is an essential part of my code, so I’m hoping I can still use it.
For example
`
testStr <- “hithere”;
server.log(“toupper=”+testStr.toupper());
server.log(“slice=”+testStr.slice(3));
server.log(“toupper.call=”+testStr.toupper.call(testStr)); // this fails
server.log(“toupper.acall=”+testStr.toupper.acall([testStr])); // this fails
`
The error I get is:
[Agent] toupper=HITHERE
[Agent] slice=here
[Agent] ERROR: parameter 1 has an invalid type ‘string’ ; expected: ‘table|userdata|class|instance|meta’
[Agent] ERROR: in unknown overrides:2
[Agent] ERROR: from main vimp:10
Squirrel call() and acall() don’t work on the built-in types. The best workaround is probably to wrap the strings in an object of your own which offers the methods that you need.
I’m looking for an easy way to determine whether I can still use obj.method.acall([params]) to invoke the method or (obj.method)(param1,param2…) solely for the built-in types.
So you’ve got a Squirrel variable of unknown type, and a string which is the name of a method you’d like to call on that variable, and an array of the parameters you’d like to pass?
One way would be to use typeof() to work out whether it’s a built-in type or not. But if you’re prepared to “manually” implement acall() in the built-in case, maybe you should do it that way in all cases?