Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: "break" and functions
- X-seq: zsh-workers 52850
- From: Oliver Kiddle <opk@xxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- Cc: Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: Re: "break" and functions
- Date: Fri, 29 Mar 2024 18:38:46 +0100
- Archived-at: <https://zsh.org/workers/52850>
- In-reply-to: <CAH+w=7bb9BKUCvqCb=A-Qw36+HVn-NszP2DfQeXsYmKs-HVWFg@mail.gmail.com>
- List-id: <zsh-workers.zsh.org>
- References: <CAH+w=7bb9BKUCvqCb=A-Qw36+HVn-NszP2DfQeXsYmKs-HVWFg@mail.gmail.com>
Bart Schaefer wrote:
> When adding this, I ran into a puzzling effect:
>
> % while :; do; echo ${|REPLY=x;break}; done
> x
> % while :; do; () { echo "$@" } ${|REPLY=x;break}; done
> %
>
> That is, breaking the loop skips the function call but does not skip
> the builtin.
My expectation would have been for either an error message saying
break: not in while, until, select, or repeat loop`
or that because the break is run before the builtin that the builtin
would never get to run. But the implementation of break is just putting
a value in the breaks integer variable so I guess that variable isn't
checked until after the builtin is run. The anonymous function is
perhaps a two-stage process for defining and then running. Note:
while :; do; () { echo hello } > ${|REPLY=x;break}; done
creates an empty file named x.
When suggesting the test, the exact behaviour was not something I cared
strongly about provided it wasn't seg fault, infinite loop or similar.
In the case of
repeat 3 print c*(oe:'break':)
it might be a useful feature if break applied to the internal loop over
globbed files causing print to run but only with those files that were
matched so far. false works as a continue in that case.
If you're choosing which of the two cases in 52151 is wrong,
I'd say that with the purr() function is right because the break is run
before the print (or purr).
And possibly, it should be an error message for "not in a while, ..."
It also rather surprises me that the break here even works:
br() { break }
while : ; do
br
done
Oliver
Messages sorted by:
Reverse Date,
Date,
Thread,
Author