Friday, January 20, 2023

Re: Using Vim9 for a function

> On Fri, Jan 20, 2023 at 1:48 AM andalou <cesar.romani@gmail.com> wrote:
> >
> >
> > 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
>
> 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.

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

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20230120182246.D70281C09EE%40moolenaar.net.

No comments:

Post a Comment