> 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
Well, in a sense that is correct. The Filter() functions accepts a
function reference with a specific type, and what is passed in is a
function reference without any known type. Using that function
reference might fail, thus requires a runtime check, which is what we
want to avoid in a compiled function.
This works:
def Func()
Filter(x, { v -> v =~ '^b' })
enddef
Func()
Because the lambda is compiled.
I suppose what we could do is that when an argument is a lambda, first
compile it, so that we have the type. This would also make execution
faster. It gets a bit more complicated if it's a nested structure, e.g.
dict with a lambda.
--
A)bort, R)etry, B)ang it with a large hammer