> 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

 /// Bram Moolenaar -- b...@moolenaar.net -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_use/202009271545.08RFjj4L186795%40masaka.moolenaar.net.

Reply via email to