Saturday, May 8, 2021

Re: Vim9: E1096 Returning a value in a function without return type

Christian wrote:

> I am getting the error in the subject in a script I am writing. I have
> found that the culprit is the use of statements in a lambda.
>
> This is a minimal example showing the problem:
>
> vim9script
>
> def Bind(x: number): func(string): any
> return (s) => x
> enddef
>
> def Bind2(x: number): func(string): any
> return (s) => {
> return x
> }
> enddef
>
> var B = Bind(42)
> var B2 = Bind2(42)
>
> While Bind() is executed correctly, Bind2() raises the error. Am I doing
> something wrong?
>
> (This is of course a contrived example: in my script, the lambda
> contains a few statements, so I must use {}).

You are missing the return type in the lambda:

def Bind2(x: number): func(string): any
return (s): any => { # ": any" added here
return x
}
enddef

Perhaps it's a bit strange that the argument doesn't require a type but
the return type is required... The problem is that in a lambda without
{} there can be only one return, the value of the expression, which is
then used for the return type. With the {} form you could have multiple
return statements, thus the compiler likes to check the type.

One way to solve this is to also require argument types for the {} form.
That way it becomes more an inline :def function than a lambda.

A more complicated solution (for the compiler) is the take the common
type of all the return statements.

Thoughts?


--
hundred-and-one symptoms of being an internet addict:
248. You sign your letters with your e-mail address instead of your name.

/// 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/202105081235.148CZrHv1627150%40masaka.moolenaar.net.

No comments: