Hi, On Thu, Sep 24, 2020 at 6:29 AM Brett Stahlman <[email protected]> wrote:
> On Thu, Sep 24, 2020 at 4:40 AM Bram Moolenaar <[email protected]> > wrote: > > > > > Brett Stahlman wrote: > > > > > > Allowing for other numbers leads to mistakes. E.g. when using "-1", > > > > is that false or true? And using 9999 for true doesn't make sense, > > > > it is probably a mistake. > > > > > > I guess because I've mostly programmed in C/C++, I think of _**any**_ > > > nonzero value as truthy. That just feels idiomatic and natural to me. > > > For instance, if I had a variable intended to hold a line number (or > > > zero to signify undefined), I would think it natural to treat the line > > > number variable as a boolean to test for definedness. But I realize > > > this sort of thing is often frowned upon in modern, strongly typed > > > languages... > > > > C is quite old and very close to CPU instructions. It is easy to make > > mistakes, e.g. checking if a pointer is NULL while you meant to check if > > what it points to is zero: > > > > char *ptr > > ... > > if (ptr) > > > > should have been "if (*ptr)" but doesn't give any warning. > > > > I understand your point, though in my opinion, setting a pointer to NULL to > indicate it doesn't currently point to anything useful is a perfectly valid > idiom; in fact, the ability of a pointer to hold 0 is the reason pointers > are occasionally preferred over references in C++. But I don't deny that > problems can result when one is careless... > > > > > > > I didn't realize things would still work as before when the variable > > > is used in a conditional. (A plugin user reported that the change was > > > breaking something in a plugin I've contributed to, and I haven't > > > actually tested with the latest version of Vim.) > > > > > > Would the "want_bool" flag be true if unary '!' is used to force > > > interpretation of a variable as a boolean? E.g., will something like > > > this continue to work as before? > > > ``` > > > let valid_line_nr = !!line_nr > > > if valid_line_nr > > > ... > > > endif > > > ``` > > > > First of all, legacy Vim script and functions don't change. This only > > applies to Vim9 script and :def functions. > > > > Ah. This is a relief. Now that I'm learning more about Vim9script, I'm > thinking this change shouldn't have caused any issues for the plugin, which > doesn't use the special "vim9script" directive. > I'm assuming that the experimental Vim9 fork will eventually be merged back > to mainline Vim, and that when that happens, legacy scripts will continue > to work normally because they don't contain the "vim9script" directive. Is > this a correct understanding? > The Vim9 support is already in the mainline Vim. It is not in a fork. Regards, Yegappan > > Thanks, > Brett S. > > > > You can indeed use "!" on any value, and it will use it as falsey/thruty > > and result in true/false. Although the example should probably be: > > > > let valid_line_nr = line_nr > 0 > > > > Since negative numbers are also invalid. And when "line_nr" happens to > > be a string you will get an error, while "!line_nr" would test if the > > string is empty. > > > > -- -- You received this message from the "vim_dev" 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_dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/vim_dev/CAAW7x7mYiMQ1gbg%3DKYVr5Z%3DENxn7NSkBPv0WZADqdZHAMUOuoA%40mail.gmail.com.
