Example code in vim9.txt doesn't work

19 views
Skip to first unread message

Yegappan Lakshmanan

unread,
Mar 20, 2022, 7:01:27 PM3/20/22
to vim_dev
Hi,

When I source the following example code fragment in vim9.txt
(under vim9-no-dict-function), I get the "E1168: Argument already
declared in the script: d" error.

-----------------------------------------------------------------
def DictFunc(d: dict<any>, arg: string)
echo d[arg]
enddef
var d = {item: 'value', func: DictFunc}
d.func(d, 'item')
-----------------------------------------------------------------

Either the example should be changed or the code should be modified
to support this usage.

I was testing the ":source" command to source vim9 code when I ran
into this problem.

- Yegappan

Bram Moolenaar

unread,
Mar 21, 2022, 7:11:18 AM3/21/22
to vim...@googlegroups.com, Yegappan Lakshmanan
Ah, we added a rule that an argument cannot shadow a script variable.
I'll change the name of the script variable.

I don't think we should change this rule for shadowing, when using "d"
in the function it might not be clear if the user intended to use the
argument or the script variable. Especially if the function gets long
and the types match.

It's actually nice that the new :source functionality makes this easy to
try out. However, it might be that two examples overlap, since this
file is a help file with unrelated examples. Perhaps we need a way to
clear what the script contains? We can't use "!", it already has a
meaning for :source. Perhaps "[range]source ++clear" ?

--
Yah, well, we had to carve our electrons out of driftwood we'd
find. In the winter. Uphill. Both ways.

/// 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 ///

Yegappan Lakshmanan

unread,
Mar 21, 2022, 10:19:21 AM3/21/22
to Bram Moolenaar, vim_dev
Hi Bram,

On Mon, Mar 21, 2022 at 4:11 AM Bram Moolenaar <Br...@moolenaar.net> wrote:
>
> It's actually nice that the new :source functionality makes this easy to
> try out.
>

It involves a little bit more typing to try out the Vim9 sample code
in the help text compared to the legacy code.

For the legacy script examples in the help text, you can visually select
the code and run the ":'<,'>source" command.

For the Vim9 script examples in the help text, you need to visually select
the code and run the ":vim9cmd :'<,'>source" command. The visual
selection range needs to be prefixed with "vim9cmd :".

>
> However, it might be that two examples overlap, since this
> file is a help file with unrelated examples. Perhaps we need a way to
> clear what the script contains? We can't use "!", it already has a
> meaning for :source. Perhaps "[range]source ++clear" ?
>

I can add support for this. But this involves more typing though.

Regards,
Yegappan

Bram Moolenaar

unread,
Mar 21, 2022, 12:21:35 PM3/21/22
to vim...@googlegroups.com, Yegappan Lakshmanan

Yegappan wrote:

> On Mon, Mar 21, 2022 at 4:11 AM Bram Moolenaar <Br...@moolenaar.net> wrote:
> >
> > It's actually nice that the new :source functionality makes this easy to
> > try out.
> >
>
> It involves a little bit more typing to try out the Vim9 sample code
> in the help text compared to the legacy code.
>
> For the legacy script examples in the help text, you can visually select
> the code and run the ":'<,'>source" command.
>
> For the Vim9 script examples in the help text, you need to visually select
> the code and run the ":vim9cmd :'<,'>source" command. The visual
> selection range needs to be prefixed with "vim9cmd :".

Yes, I was wondering whether we can allow using ":'<,'>vim9cmd source".
Or in general, ":'<,'>cmdmod command". Since the "'<,'>" is
automatically added. We probably can't do this for ranges in general.

> > However, it might be that two examples overlap, since this
> > file is a help file with unrelated examples. Perhaps we need a way to
> > clear what the script contains? We can't use "!", it already has a
> > meaning for :source. Perhaps "[range]source ++clear" ?
>
> I can add support for this. But this involves more typing though.

Well, otherwise you might get an error.

--
Luxury. We used to have to get out of the lake at three o'clock in the
morning, clean the lake, eat a handful of hot gravel, go to work at the
mill every day for tuppence a month, come home, and Dad would beat us
around the head and neck with a broken bottle, if we were LUCKY!
Reply all
Reply to author
Forward
0 new messages