Re: Vim9: rethinking conditions and boolean expressions

2020-10-05 Fir de Conversatie Ben Jackson
> The "??" operator is used in TypeScript, JavaScript, C# and a few other
> 
> languages.

Oh ok, fair enough, thanks. Shows how much I know :) I’d never seen it before.

> On 5 Oct 2020, at 14:20, Bram Moolenaar  wrote:
> 
> 
>>> var name = Getname() ?? 'unknown' 
>> 
>> This `??` operator seems a bit unique to vim9script. If we're trying to be 
>> more like other languages how about either re-using `else` (as in Getname() 
>> else 'unknown') or using `or` (Like python): `Getname() or "unknown"`
> 
> Python operators are words, which I find making an expression harder to
> understand.  We don't have any operators that are words, I don't see a
> reason to use it here.
> 
> The "??" operator is used in TypeScript, JavaScript, C# and a few other
> languages.
> 
> -- 
> hundred-and-one symptoms of being an internet addict:
> 35. Your husband tells you he's had that beard for 2 months.
> 
> /// Bram Moolenaar -- b...@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///

-- 
-- 
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 vim_dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/D96FFEAA-B17B-4CF5-BB68-2DD836329B24%40gmail.com.


Re: Vim9: rethinking conditions and boolean expressions

2020-10-05 Fir de Conversatie Bram Moolenaar


> > var name = Getname() ?? 'unknown' 
> 
> This `??` operator seems a bit unique to vim9script. If we're trying to be 
> more like other languages how about either re-using `else` (as in Getname() 
> else 'unknown') or using `or` (Like python): `Getname() or "unknown"`

Python operators are words, which I find making an expression harder to
understand.  We don't have any operators that are words, I don't see a
reason to use it here.

The "??" operator is used in TypeScript, JavaScript, C# and a few other
languages.

-- 
hundred-and-one symptoms of being an internet addict:
35. Your husband tells you he's had that beard for 2 months.

 /// Bram Moolenaar -- b...@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///

-- 
-- 
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 vim_dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/202010051320.095DKXB62406083%40masaka.moolenaar.net.


Re: Vim9: rethinking conditions and boolean expressions

2020-10-05 Fir de Conversatie Bram Moolenaar


Dominique wrote:

> Bram Moolenaar  wrote:
> 
> > For the first example, we can use a new operator which is specifically
> > for testing an expression to be falsy and using a replacement:
> >
> > var name = Getname() ?? 'unknown'
> >
> > Let me know if you have comments.
> 
> This is known as the 'elvis operator', which exists
> in several languages.  See:
> 
> https://en.wikipedia.org/wiki/Elvis_operator
> 
> Generally, it is ?: as in:
> 
>   var name = Getname() ?: 'unknown';
> 
> It may be worth saying that it is almost equivalent to
> the longer:
> 
>   var name = Getname() ? Getname() : 'unknown';
> 
> ... except that in the case of the elvis operator,
> Gename() is guaranteed to be invoked only once,
> which may not only be more efficient, but can also
> be different if Getname() had side effects.
> 
> This image explains why it's called the 'elvis operator':
> 
> https://i.stack.imgur.com/bVG64.png

What I intend to do here makes a difference between the two operators,
which is also why I prefer using "??" instead of "?:".  The latter
suggest the equivalence with "cond ? expr : expr", which is not quite
right.

The condition in "cond ? expr : expr" is just like a condition used for
"if": it should evaluate to a boolean.  The ternary expression is a
short way of doing if-then-else, thus using the same semantics for the
condition makes sense.

For the "??" operator, which I prefer calling the falsy-operator, the
expression is not required to evaluate to a boolean.  In fact, it's most
useful if it doesn't.  It can be a string, list, dict, etc.  And
evaluating it will never result in an error, every value is either
truthy or falsy.

A name sometimes used is "nullish coalescing operator", which is not
only hard to type but also not quite the same.  "nullish" and "falsy"
have different semantics.  We could call it the "falsy coalescing
operator", but "falsy operator" works just as well and is a lot easier
to type.  Still, "nullish coalescing operator" comes closest, thus using
"??" as the operator will be recognized by most users. I hope the
different semantics won't cause too much confusion.

-- 
hundred-and-one symptoms of being an internet addict:
34. You laugh at people with a 10 Mbit connection.

 /// Bram Moolenaar -- b...@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///

-- 
-- 
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 vim_dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/202010051320.095DKWlx2406071%40masaka.moolenaar.net.


Re: Vim9: rethinking conditions and boolean expressions

2020-10-05 Fir de Conversatie Dominique Pellé
Bram Moolenaar  wrote:

> For the first example, we can use a new operator which is specifically
> for testing an expression to be falsy and using a replacement:
>
> var name = Getname() ?? 'unknown'
>
> Let me know if you have comments.

This is known as the 'elvis operator', which exists
in several languages.  See:

https://en.wikipedia.org/wiki/Elvis_operator

Generally, it is ?: as in:

  var name = Getname() ?: 'unknown';

It may be worth saying that it is almost equivalent to
the longer:

  var name = Getname() ? Getname() : 'unknown';

... except that in the case of the elvis operator,
Gename() is guaranteed to be invoked only once,
which may not only be more efficient, but can also
be different if Getname() had side effects.

This image explains why it's called the 'elvis operator':

https://i.stack.imgur.com/bVG64.png

Regards
Dominique

-- 
-- 
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 vim_dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/CAON-T_hAu7PD1%2Bx4LW8DZhTtrVf6j7FM%2BCKrKS1moqoTgHd29g%40mail.gmail.com.


Re: Vim9: rethinking conditions and boolean expressions

2020-10-05 Fir de Conversatie puremo...@gmail.com
> var name = Getname() ?? 'unknown' 

This `??` operator seems a bit unique to vim9script. If we're trying to be 
more like other languages how about either re-using `else` (as in Getname() 
else 'unknown') or using `or` (Like python): `Getname() or "unknown"`


On Sunday, October 4, 2020 at 10:47:15 PM UTC+1 Bram Moolenaar wrote:

>
> > In javascript you can use !! operator to always convert it to boolean.
> > 
> > var name = ''
> > var nameExists = !!(name || 'Prabir')
>
> Yes, and I think we should do the same. It should already work like
> that now.
>
> Thus in most places where a condition is expected, such as with ":if"
> and ":while", a boolean is expected. With legacy script you could use a
> string, which was converted to a number and easily leads to mistakes.
> Also numbers could be used. With Vim9 script only the numbers zero and
> one can be used. This avoids making mistakes, but it's a bit strict.
>
> The new "??" operator can be used with any expression. Just like with
> "!" accepts any expression. In these places the expression is tested to
> be "falsy" or "truthy".
>
> I might have missed something, we might need a few more tests.
>
>
> -- 
> hundred-and-one symptoms of being an internet addict:
> 32. You don't know what sex three of your closest friends are, because they
> have neutral nicknames and you never bothered to ask.
>
> /// 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 ///
>

-- 
-- 
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 vim_dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/b0bcb397-317c-4fa7-b42f-5ac6eebd3402n%40googlegroups.com.


Re: Vim9: rethinking conditions and boolean expressions

2020-10-04 Fir de Conversatie Bram Moolenaar


> In javascript you can use !! operator to always convert it to boolean.
> 
> var name = ''
> var nameExists = !!(name || 'Prabir')

Yes, and I think we should do the same.  It should already work like
that now.

Thus in most places where a condition is expected, such as with ":if"
and ":while", a boolean is expected.  With legacy script you could use a
string, which was converted to a number and easily leads to mistakes.
Also numbers could be used.  With Vim9 script only the numbers zero and
one can be used.  This avoids making mistakes, but it's a bit strict.

The new "??" operator can be used with any expression.  Just like with
"!" accepts any expression.  In these places the expression is tested to
be "falsy" or "truthy".

I might have missed something, we might need a few more tests.


-- 
hundred-and-one symptoms of being an internet addict:
32. You don't know what sex three of your closest friends are, because they
have neutral nicknames and you never bothered to ask.

 /// Bram Moolenaar -- b...@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///

-- 
-- 
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 vim_dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/202010042147.094Ll6Gh2232566%40masaka.moolenaar.net.


Re: Vim9: rethinking conditions and boolean expressions

2020-10-04 Fir de Conversatie ma...@prabir.me
In javascript you can use !! operator to always convert it to boolean.

var name = ''
var nameExists = !!(name || 'Prabir')

On Saturday, October 3, 2020 at 12:25:21 PM UTC-7 Andy Wokula wrote:

> Am 03.10.2020 um 17:44 schrieb Bram Moolenaar:
> > Therefore, I'm going to make && and || have a boolean result again.
> > I think that is the easiest to understand, and what most languages do.
>
> +100
>
> -- 
> Andy
>

-- 
-- 
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 vim_dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/d82bdde2-4327-4907-a866-d57faf95ed2dn%40googlegroups.com.


Re: Vim9: rethinking conditions and boolean expressions

2020-10-03 Fir de Conversatie 'Andy Wokula' via vim_dev

Am 03.10.2020 um 17:44 schrieb Bram Moolenaar:

Therefore, I'm going to make && and || have a boolean result again.
I think that is the easiest to understand, and what most languages do.


+100

--
Andy

--
--
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 vim_dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/5F78D00C.3010704%40yahoo.de.


Vim9: rethinking conditions and boolean expressions

2020-10-03 Fir de Conversatie Bram Moolenaar


As a first approach I have used what TypeScript does.  However,
considering the feedback I have received, it's not very consistent and
has a few surprising sides.

One thing is that the "||" and "&&" operators do not result in a boolean
true or false, but the argument value.  This is nice for something like:

var name = Getname() || 'unknown'

When the result of Getname() is falsy (null or empty) then the result
is "unknown".

However, when the result should be a boolean, this does not work:

var flag: bool = Getname() || false

On top of that, the order matters, this works:

var flag: bool = Getname() && true

But this doesn't:

var flag: bool = true && Getname()

That is unexpected if you consider && to be a boolean expression.


Therefore, I'm going to make && and || have a boolean result again.
I think that is the easiest to understand, and what most languages do.

For the first example, we can use a new operator which is specifically
for testing an expression to be falsy and using a replacement:

var name = Getname() ?? 'unknown'

Let me know if you have comments.

-- 
One difference between a man and a machine is that a machine is quiet
when well oiled.

 /// Bram Moolenaar -- b...@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///

-- 
-- 
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 vim_dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/202010031544.093FiA401865731%40masaka.moolenaar.net.