E123 when referring to function argument

9 views
Skip to first unread message

Matan Nassau

unread,
May 22, 2023, 8:04:04 PM5/22/23
to v...@vim.org
Hello.

I want to peek into a lambda so I can evaluate it in a new Vim process. Why do I get 'E123: Undefined function: Op' when I source this script?

vim9script
def! g:LambdaText(Op: func(): number): void
    func Op
enddef

def! g:TestRun(): void
    g:LambdaText(() => 2 * 4)
enddef

g:TestRun()


Matan

Bram Moolenaar

unread,
May 23, 2023, 9:48:30 AM5/23/23
to vim...@googlegroups.com, Matan Nassau
The argument of the "func" command is a function name, not an
expression. You are passing a variable, which is an expression.

There is a trick you can use: Assign the function reference to a global
variable. Function names exist in the global namespace where the global
variable can also be found:

def g:LambdaText(Op: func(): number): void
g:ThatFunction = Op
func g:ThatFunction
unlet g:ThatFunction
enddef


--
Be thankful to be in a traffic jam, because it means you own a car.

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

Matan Nassau

unread,
May 23, 2023, 11:36:02 AM5/23/23
to Bram Moolenaar, vim...@googlegroups.com

On May 23, 2023, at 09:48, Bram Moolenaar <Br...@moolenaar.net> wrote:

The argument of the "func" command is a function name, not an
expression.  You are passing a variable, which is an expression.

There is a trick you can use: Assign the function reference to a global
variable.  Function names exist in the global namespace where the global
variable can also be found:

    def g:LambdaText(Op: func(): number): void
        g:ThatFunction = Op
        func g:ThatFunction
        unlet g:ThatFunction
    enddef

Thank you, this clarifies things.

I also learned I can call string(function(Op)) to get the lambda’s name, and then call execute(‘func ‘ .. lambda_name) to get the lambda’s body.


Matan
Reply all
Reply to author
Forward
0 new messages