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

35 views
Skip to first unread message

Lifepillar

unread,
May 8, 2021, 6:49:48 AM5/8/21
to vim...@googlegroups.com
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 {}).

Thanks,
Life.


Bram Moolenaar

unread,
May 8, 2021, 8:36:05 AM5/8/21
to vim...@googlegroups.com, Lifepillar
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 -- 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 ///

Lifepillar

unread,
May 8, 2021, 8:53:44 AM5/8/21
to vim...@googlegroups.com
On 2021-05-08, Bram Moolenaar <Br...@moolenaar.net> wrote:
>
> 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

Thanks, that worked!

> Perhaps it's a bit strange that the argument doesn't require a type but
> the return type is required...

That's fine with me. The only issue is that I don't think that this is
documented yet (unless I missed it).

Thanks,
Life.

Reply all
Reply to author
Forward
0 new messages