Vim9: call function from variadic function?

60 views
Skip to first unread message

Lifepillar

unread,
Feb 28, 2021, 9:14:24 AM2/28/21
to vim...@googlegroups.com
This might be a bit of a stretch, but... is it possible to
"expand/split" variable arguments to call another function with fixed
arguments? I would like to define a function that takes as input another
function F and some values v1, vn... , and applies F to v1, ..., vn.

For example, I can define:

def F(a: string, b: number)
enddef

def G(...values: list<any>): void
F(values[0], values[1])
enddef

G('xyz', 42)

I would like to generalize G so that it can invoke any function, i.e.:

def G2(F: func, ...values: list<any>): void
F(XXXX)
enddef

G2(F, 'xyz', 42)

The problem is: what can I put in place of XXXX? F(values) doesn't cut
it.

Thanks,
Life.


Stan Brown

unread,
Feb 28, 2021, 11:28:16 PM2/28/21
to vim...@googlegroups.com
:h curly-braces-names

--
Stan Brown
Tehachapi, CA, USA
https://BrownMath.com
https://OakRoadSystems.com

Bram Moolenaar

unread,
Mar 1, 2021, 7:54:16 AM3/1/21
to vim...@googlegroups.com, Stan Brown

Stan Brown wrote:

> :h curly-braces-names

Note that in Vim9 script curly-braces are not supported. They have
always been a bit problematic, and compiling an expression with
curly-braces is not possible, it needs to be interpreted.

--
Violators can be fined, arrested or jailed for making ugly faces at a dog.
[real standing law in Oklahoma, United States of America]

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Lifepillar

unread,
Mar 1, 2021, 12:54:33 PM3/1/21
to vim...@googlegroups.com
On 2021-03-01, Bram Moolenaar <Br...@moolenaar.net> wrote:
>
> Stan Brown wrote:
>
>> :h curly-braces-names

Thanks, I didn't know about that feature.

> Note that in Vim9 script curly-braces are not supported. They have
> always been a bit problematic, and compiling an expression with
> curly-braces is not possible, it needs to be interpreted.

As I said, what I was trying to do is a bit of a stretch. I have found
a different way to approach my problem.

Thanks for the feedback,
Life.

Bram Moolenaar

unread,
Mar 1, 2021, 1:24:49 PM3/1/21
to vim...@googlegroups.com, Lifepillar
Why not use call():
call(F, ['xyz', 42])

Or, if you need G2() to do something more:
def G2(F: func, ...values: list<any>): void
call(F, values)
enddef

--
No man may purchase alcohol without written consent from his wife.
[real standing law in Pennsylvania, United States of America]

Lifepillar

unread,
Mar 1, 2021, 1:39:25 PM3/1/21
to vim...@googlegroups.com
On 2021-03-01, Bram Moolenaar <Br...@moolenaar.net> wrote:
>
>
>> This might be a bit of a stretch, but... is it possible to
>> "expand/split" variable arguments to call another function with fixed
>> arguments? I would like to define a function that takes as input another
>> function F and some values v1, vn... , and applies F to v1, ..., vn.
>>
>> For example, I can define:
>>
>> def F(a: string, b: number)
>> enddef
>>
>> def G(...values: list<any>): void
>> F(values[0], values[1])
>> enddef
>>
>> G('xyz', 42)
>>
>> I would like to generalize G so that it can invoke any function, i.e.:
>>
>> def G2(F: func, ...values: list<any>): void
>> F(XXXX)
>> enddef
>>
>> G2(F, 'xyz', 42)
>>
>> The problem is: what can I put in place of XXXX? F(values) doesn't cut
>> it.
>
> Why not use call():
> call(F, ['xyz', 42])

Ah, I had forgotten about call()!

> Or, if you need G2() to do something more:
> def G2(F: func, ...values: list<any>): void
> call(F, values)
> enddef

Yes, that does exactly what I have asked for.

Thanks!
Life.

Reply all
Reply to author
Forward
0 new messages