Wednesday, August 17, 2022

Re: [vim9script] Behavior of has_key()

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

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

--
--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20220817123218.7A0F71C0B04%40moolenaar.net.

No comments: