Using Vim9 for a function

瀏覽次數:9 次
跳到第一則未讀訊息

andalou

未讀,
2023年1月19日 晚上7:48:062023/1/19
收件者: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

未讀,
2023年1月20日 上午11:27:002023/1/20
收件者: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

未讀,
2023年1月20日 下午1:22:592023/1/20
收件者: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 ///
回覆所有人
回覆作者
轉寄
0 則新訊息