Sunday, September 27, 2020

Re: Lambdas and "typedefs" in Vim9

On 2020-09-27, Bram Moolenaar <Bram@moolenaar.net> wrote:

>> I have two questions/curiosities:
>>
>> 1. I was not able to pass lambdas to defs, only funcrefs. Can/will this
>> be supported?
>
> Can you give an example of what does not work?

This is what I have tried:

vim9script

def Filter(l: list<string>, Cond: func(string): bool): list<string>
let res = []
for item in l
if Cond(item)
add(res, item)
endif
endfor
return res
enddef

def Filter2(l: list<string>, Cond: func(string): bool): list<string>
return filter(copy(l), Cond)
enddef

def MyCond(v: string): bool
return v =~ '^a'
enddef

let x = ['a', 'b', 'c']

Filter(x, MyCond)
# OK

Filter(x, funcref(MyCond))
# OK

Filter(x, { v -> v =~ '^b' })
# E1013

Filter2(x, { v -> v =~ '^b' })
# Ditto

I have tried other variations of the signatures, such as 'func', but
without luck. Also tried to add types to the lambda, as in:

{ v: string -> ... }

but that gives E720 (not that I expected it to work).

>> 2. Has a type aliasing or "typedef" mechanism been considered or,
>> perhaps, already planned? My use case is defining a library of opaque
>> types, which I can change without the need to modify client code.
>
> Yes, typedefs are planned to be added at some point. We can make
> everything work without it, so it's for later.

That's great!

Life.

--
--
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/rkq4el%24p4m%241%40ciao.gmane.io.

No comments: