squirrel icon indicating copy to clipboard operation
squirrel copied to clipboard

Forwarding vargv as vargv and not an array

Open breakin opened this issue 7 years ago • 4 comments

I tried the following code that felt natural to me:

local p = @(...) my_function(format(vargv));

This failed since format wants a (...) list but vargv is an array.

As a python user I tried *vargv hoping it would turn it back into a parameter list. Afaict that is the python way to solve this this issue (https://stackoverflow.com/questions/2921847/what-does-the-star-operator-mean).

I can modify the native format-function to accept an array so I am not blocked by this, but it would be nice to know if this can be done in squirrel script. If not consider this a feature request!

breakin avatar Nov 23 '17 21:11 breakin

It seems like a solution is function.acall. The following works:

a=["dummy", "%d %d %d", 1,2,3]; print(format.acall(a));

It would be nice with a version that inherited the environment so that the first array element does not have to be this.

breakin avatar Nov 23 '17 22:11 breakin

I feel sure the only way for your nice-to-have is as another version which receives the environment as a separate parameter. Inheriting it automatically doesnt make sense here; avoiding rejiggering the array so this can be first does.

zeromus avatar Nov 23 '17 22:11 zeromus

@zeromus: I am quite the beginner at squirrel so this question might be naive. But the function I want to call in this case is format. When I call it "normally" (not using acall) I just write

format("%d,%d,%d",1,2,3);

If there is a this-pointer or an environment it is supplied implicitly. This I expect my new function to look like this:

local a=["%d %d %d", 1,2,3]; format.new_call(a);

Am I missing something? If I had an extra this pointer I could give separately, what would I give it to make it work like format does here?

breakin avatar Nov 23 '17 22:11 breakin

It isn't clear to me what the environment would be for new_call indexed off a closure.

if we did myobj.some_call(a) then some_call's environment would be myobj. with myclosure.some_call(a) then what's the environment? to get the behaviour you want, a_call would have to look at its caller's environment. This is weird and maybe unprecedented?

For what it's worth, your thought process is sensible anyway: why not do the straightforward transformation? I just explained why.

Check out c#'s Method.Invoke() which receives a this and then an array of arguments. That's what I suggested. If it were my design, squirrel would have worked that way to begin with. But there's a sensible reason it didn't: in many places conceptually in squirrel, this is the first argument. That paradigm doesnt really rule in c#.

zeromus avatar Nov 23 '17 23:11 zeromus