vim9 exists() can't test whether a dictionary value is set in a function argument

8 views
Skip to first unread message

Christian J. Robinson

unread,
Jul 27, 2021, 7:10:07 PM7/27/21
to vim_dev, bu...@vim.org

    vim9script

 

    def Foo(bar: dict<string>)

        if exists('bar["a"]')

            echo "it exists"

        else

            echo "it doesn't exist"

        endif

    enddef

 

    Foo({'a': '...'})

    Foo({'b': '...'})

 

This should echo:

 

    it exists

    it doesn’t exist

 

But it echoes:

 

    it doesn’t exist

    it doesn’t exist

 

I have tried all kinds of combinations, including checking for l:bar[“a”] and a:bar[“a”].  The only thing that works is to assign the bar argument to a global variable and testing that instead, but that’s a kluge.

 

Regards,

Christian

 

Bram Moolenaar

unread,
Jul 28, 2021, 5:52:35 AM7/28/21
to vim...@googlegroups.com, Christian J. Robinson, bu...@vim.org
Correct, function arguments are not visibile to exists() in a compiled
function. I notice that this isn't mentioned in the help, I'll update
that.

You should has has_key():

if bar->has_key("a")
echo 'it exists'


--
hundred-and-one symptoms of being an internet addict:
255. You work for a newspaper and your editor asks you to write an
article about Internet addiction...in the "first person."

/// 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 ///
Reply all
Reply to author
Forward
0 new messages