zsh-users
 help / color / mirror / code / Atom feed
From: Roman Perepelitsa <roman.perepelitsa@gmail.com>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: unintuitive bracketing with return value
Date: Tue, 18 Oct 2022 18:07:42 +0200	[thread overview]
Message-ID: <CAN=4vMpYvUNUmv5BqgEe5qGmvhtYTiH+bBUkzf_O5_vvYZzs3w@mail.gmail.com> (raw)
In-Reply-To: <4b6eb0b7-ae82-a81f-6a70-784ffc1d39ff@eastlink.ca>

On Tue, Oct 18, 2022 at 4:47 PM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> [...] shouldn't the brackets do the intuitive thing and
> force the final '||' to be the alternative to the first '&&'?

You seem to be thinking of `X && Y || Z` as a single construct but
there is no such thing. There is only `X && Y` and `X || Y`. In any
programming language I'm familiar with, these operators are
left-associative, short-circuit, and the result is always that of the
last evaluated operand. Short-circuiting means that the second operand
is evaluated by &&/|| if and only if the first operand is true/false.
There are two aspects on which programming languages differ when it
comes to these operators.

1. Is the result coerced to bool (0 or 1)? Yes in C (and other
statically-typed languages), no in Zsh (and other dynamically-typed
languages).
2. Does && have higher precedence than ||? Yes in C (and most
languages), no in Zsh (and all shells).

Once you know this, it should be clear why your program evaluates the
way it does.

So, how do you put this in practice? Use `if` when you need `if`. Use
`X && Y || Z` only when `if` would be wrong. However, even then it's
almost always better to do this:

  if ! X || ! Y; then
    Z
  fi

Roman.


      parent reply	other threads:[~2022-10-18 16:08 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-18 14:47 Ray Andrews
2022-10-18 14:57 ` Eric Cook
2022-10-18 17:57   ` Ray Andrews
2022-10-18 19:32     ` Lawrence Velázquez
2022-10-18 20:12       ` Bart Schaefer
2022-10-18 20:14         ` Bart Schaefer
2022-10-18 20:22       ` Ray Andrews
2022-10-18 20:31         ` Clinton Bunch
2022-10-18 16:07 ` Roman Perepelitsa [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAN=4vMpYvUNUmv5BqgEe5qGmvhtYTiH+bBUkzf_O5_vvYZzs3w@mail.gmail.com' \
    --to=roman.perepelitsa@gmail.com \
    --cc=rayandrews@eastlink.ca \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).