Using Vim9 for a function

9 views
Skip to first unread message

andalou

unread,
Jan 19, 2023, 7:48:06 PM1/19/23
to vim_use

I'm using vim 9.1221 on Windows 10. I tried to create a function using
Vim9, as in:

def! Foo(a: float, b: float): float
  return a + b
enddef

If I call Foo(1.2, 5.0) I get the answer 6.2

but if I call Foo(1.2, 5) I get:

E1013: Argument 2:type mismatch, expected float but got number

How can I modify the function to make it possible to call Foo(1.2, 5)
and get a result.

Many thanks in advance,

--
Cesar

Tony Mechelynck

unread,
Jan 20, 2023, 11:27:00 AM1/20/23
to vim...@googlegroups.com
IIUC, allowing a Number to be used where a Float is expected is
permitted in Vim pre-9 but forbidden in Vim 9.

Solution 1: Use "function" instead of "def". This will make your
function a "legacy function" and it won't need strict-typed arguments:

:function Foo(a, b)
:return a + b
:endfunction

Solution 2: call Foo(1.2, 5.0) instead.

Best regards,
Tony.

Bram Moolenaar

unread,
Jan 20, 2023, 1:22:59 PM1/20/23
to vim...@googlegroups.com, Tony Mechelynck
I believe most users expect an integer number to work where a float is
expected. We encountered this before, then it was causing an invalid
result and it was solved by giving an error message. I'll make another
change that actually converts the number to a float, that should better
match expectations.


--
If you put seven of the most talented OSS developers in a room
and ask them to fix a bug in a spreadsheet program, in one week
you'd have 2 new mail readers and a text-based web browser.

/// 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 ///
Reply all
Reply to author
Forward
0 new messages