zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Mikael Magnusson <mikachu@gmail.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: [PATCH?] Nofork and removing newlines
Date: Wed, 6 Mar 2024 20:53:28 -0800	[thread overview]
Message-ID: <CAH+w=7Yq7e7JOiN51=vQjUvS+Yi6xY0VAJUGjoraeBKKazAnkw@mail.gmail.com> (raw)
In-Reply-To: <CAHYJk3Tmh0xVQodHuXycDDv21_xgkhq5hvGZgPrZbLUYox2bGA@mail.gmail.com>

On Wed, Mar 6, 2024 at 2:22 PM Mikael Magnusson <mikachu@gmail.com> wrote:
>
> 1) $(foo) will optimize away an extra fork if foo is an external command
> 2) ${ foo } will fork the same amount of times as 1) if foo is
> external and not at all if foo is a function.

You're almost quoting the FAQ entry. :-)

> "${ foo}" and ${ foo} having the same wordsplitting behavior but only
> differing in stripping newlines feels a bit magical and weird.

One question (and sort of the point) is whether anyone would really
notice.  If you put it in quotes you're expecting a literal result,
and if you (for example) assign it unquoted to a scalar you're
expecting it to "just work" the way assigning $(foo) would.  It's a
bit unusual but it seems to preserve the principle of least surprise,
and it uses the least amount of extra syntax.

On the other hand I'm not highly invested in this.  In the absence of
this (no)quoting behavior, I've found I nearly always want ${=${ foo
}} or ${(f)${ foo }}, each of which gives exactly the same result with
or without trimming.

> We could in theory add some new () flag, T for trim is free eg,
> ${(T)${ foo}} is somewhat more ergonomic than ${${ foo}%$'\n'}

I admittedly (still pre-patch) have used (f) for this when I know
there's only one line of output.  I'm just struggling to think of
where else I would use a (T).

Returning to this other bit ...

On Wed, Mar 6, 2024 at 2:42 PM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> On Wed, Mar 6, 2024 at 2:22 PM Mikael Magnusson <mikachu@gmail.com> wrote:
> >
> > Is there some strong reason we could not allow ${(T) foo} btw?
>
> "{ " (curly bracket followed by space) is recognized like a syntax
> token.

Code-wise, a sequence starting with ${ (with or without the space) and
ending with } is lexed into a single STRING token.  (If it's inside
double quotes, the entire double-quoted thing is a STRING token, but
you can have nested quotes inside the dollar-brace inside the double
quotes, etc., so this has to work recursively, and so on.)  So the
lexer has to decide when it sees dollar-brace how to find the closing
brace.  Skipping over parameter flags before deciding to switch to
parsing something that looks like a function body might be possible,
but doesn't really fit into the structure of the lexer.  Deciding
based on the very next character (space or pipe for a command, or any
other for a parameter) makes it tractable.

The lexical problem aside, when you get to the point of performing the
substitution, even if the command interpretation were deferred until
after all the flags are collected, it would still have to function
much like ${(flags)"$(cmdsubst)"} would, so it's a lot easier if it's
already structured as a nested substitution.


  parent reply	other threads:[~2024-03-07  4:54 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-05  5:52 Bart Schaefer
2024-03-05  6:56 ` Stephane Chazelas
2024-03-05 22:48   ` Bart Schaefer
2024-03-06 17:57     ` Stephane Chazelas
2024-03-06 19:45       ` Bart Schaefer
2024-03-06 22:22         ` Mikael Magnusson
2024-03-06 22:42           ` Bart Schaefer
2024-03-07  4:53           ` Bart Schaefer [this message]
2024-03-07  7:02             ` Lawrence Velázquez
2024-03-07  8:09               ` ${<file} (Was: [PATCH?] Nofork and removing newlines) Stephane Chazelas
2024-03-08  1:29               ` [PATCH?] Nofork and removing newlines Bart Schaefer
2024-03-08 22:15                 ` Oliver Kiddle
2024-03-08 23:28                   ` Bart Schaefer
2024-03-09 20:43                     ` Oliver Kiddle
2024-03-10  6:11                       ` Bart Schaefer
2024-03-12 17:54                         ` Bart Schaefer
2024-03-12 23:19                           ` Oliver Kiddle
2024-03-13  4:13                             ` Bart Schaefer
2024-03-14 22:15                               ` Oliver Kiddle
2024-03-15  8:42                                 ` Stephane Chazelas
2024-03-27  1:16                                   ` Bart Schaefer
2024-03-27  7:05                                 ` Bart Schaefer
2024-03-07  7:10             ` Stephane Chazelas
2024-03-08  0:37               ` Bart Schaefer
2024-03-07  6:52           ` Lawrence Velázquez
2024-03-07  8:26             ` Mikael Magnusson
2024-03-07 19:02               ` Bart Schaefer
2024-04-02  6:45                 ` Lawrence Velázquez
2024-03-06 19:43     ` Stephane Chazelas

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='CAH+w=7Yq7e7JOiN51=vQjUvS+Yi6xY0VAJUGjoraeBKKazAnkw@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=mikachu@gmail.com \
    --cc=zsh-workers@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).