Vim9: Not able to add a new field to a dict which is a dict member

9 views
Skip to first unread message

Yegappan Lakshmanan

unread,
Dec 6, 2020, 11:46:44 AM12/6/20
to vim_dev
Hi,

The following Vim9 function fails with "E1141: Indexable type required":

======================================================
vim9script

def g:Test()
    var req = {}
    req.params = {}
    req.params.id = 10
    echomsg req
enddef
call g:Test()
======================================================

The following legacy function works:

======================================================
func g:Test()
    let req = {}
    let req.params = {}
    let req.params.id = 10
    echomsg req
endfunc
call g:Test()
======================================================

Am I missing something here?

Thanks,
Yegappan

Bram Moolenaar

unread,
Dec 7, 2020, 12:54:49 PM12/7/20
to vim...@googlegroups.com, Yegappan Lakshmanan
The type of "req" is not specified. You initialize it with {}, which is
a dict with unknown member type. You assign it a dict in the second
line, but that doesn't change the type. In the third line you try to
access a member of an unknown. At this point we expect a list or a
dict, with an unknown we don't know what to do.

We could assume the ".id" indicates it should be a dict and turn this
into a runtime type check, but that may hide problems. Best is to
declare the type of "req":

var req: dict<dict<number>> = {}

Then it works.

--
Q: What kind of stuff do you do?
A: I collect hobbies.

/// Bram Moolenaar -- Br...@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 ///
Reply all
Reply to author
Forward
0 new messages