[vim9script] Behavior of has_key()

25 views
Skip to first unread message

Lifepillar

unread,
Aug 17, 2022, 3:22:10 AM8/17/22
to vim...@googlegroups.com
The help entry for has_key() says that the key must be a string. In
legacy Vim script, however, this works fine:

let dd = {42: '42', v:true: 'T', 3.14: '3.14'}
echo dd->has_key(42)
echo dd->has_key(v:true)
echo dd->has_key(3.14)

In Vim 9 script, the behavior is different:

vim9script
const dd = {42: '42', true: 'T', [3.14]: '3.14'}
echo dd->has_key(42) # OK
echo dd->has_key(v:true) # Error, must be string(v:true)
echo dd->has_key(3.14) # Error, must be string(3.14)

This seems a bit inconsistent to me. I don't have a problem with Vim
9 script being different from legacy script, but I think that either all
the three cases should raise an error, or none of them.

Besides, the documentation does not make it clear that implicit type
casting may happen.

Life.


Bram Moolenaar

unread,
Aug 17, 2022, 8:32:29 AM8/17/22
to vim...@googlegroups.com, Lifepillar
I'll add a note to the help.

In Vim9 script there is no automatic conversion to string, but an
exception is made for a number. I don't recall the discussion, but I
think this was because it is rather common. Perhaps not totally
consistent, but I don't want to change it now, it would cause obscure
problems.

--
A poem: read aloud:

<> !*''# Waka waka bang splat tick tick hash,
^"`$$- Caret quote back-tick dollar dollar dash,
!*=@$_ Bang splat equal at dollar under-score,
%*<> ~#4 Percent splat waka waka tilde number four,
&[]../ Ampersand bracket bracket dot dot slash,
|{,,SYSTEM HALTED Vertical-bar curly-bracket comma comma CRASH.

Fred Bremmer and Steve Kroese (Calvin College & Seminary of Grand Rapids, MI.)

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