vim9script function forward reference

12 views
Skip to first unread message

Ernie Rael

unread,
Apr 2, 2022, 1:07:52 PM4/2/22
to vim...@googlegroups.com
I've tried different things, the following looked like a good shot, but
it fails. Can a forward reference be done?

vim9script

var X: func = funcref('F')

def F()
    echo "in F()"
enddef

X()

line    3:
E700: Unknown function: F

-ernie

Bram Moolenaar

unread,
Apr 2, 2022, 4:44:02 PM4/2/22
to vim...@googlegroups.com, Ernie Rael

Ernie Rael wrote:

> I've tried different things, the following looked like a good shot, but=20
> it fails. Can a forward reference be done?
>
> vim9script
>
> var X: func = funcref('F')
>
> def F()
>     echo "in F()"
> enddef
>
> X()
>
> line    3:
> E700: Unknown function: F

Not really. You can declare the "X" variable and set it only after
defining F() though. You can't use it earlier anyway.
If you really need to you could store the name of the function as a
string and use funcref() where you need to use it.

--
BEDEVERE: Oooooh!
LAUNCELOT: No "Aaaaarrrrrrggghhh ... " at the back of the throat.
BEDEVERE: No! "Oooooh!" in surprise and alarm!
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

/// 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 ///

Ernie Rael

unread,
Apr 2, 2022, 4:58:33 PM4/2/22
to vim...@googlegroups.com
On 4/2/22 1:43 PM, Bram Moolenaar wrote:
> Ernie Rael wrote:
>
>> I've tried different things, the following looked like a good shot, but=20
>> it fails. Can a forward reference be done?
>>
>> vim9script
>>
>> var X: func = funcref('F')
>>
>> def F()
>>     echo "in F()"
>> enddef
>>
>> X()
>>
>> line    3:
>> E700: Unknown function: F
> Not really. You can declare the "X" variable and set it only after
> defining F() though. You can't use it earlier anyway.
> If you really need to you could store the name of the function as a
> string and use funcref() where you need to use it.
>
Thanks, now I understand a little better. I'm not sure I'll need it,
and glad to know how. Previously I look at help for funcref, "Just
like function()" looked at help for function(). My eyes glazed over...

-ernie

Reply all
Reply to author
Forward
0 new messages