Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: anonymous function question



On Mon, Aug 22, 2022, at 10:11 PM, Jim wrote:
> Is this normal for an anonymous function?

This is not just about anonymous functions.  You can observe the
same behavior with regular functions and other complex commands.

	% unsetopt INTERACTIVE_COMMENTS
	% foo() {
	function> setopt INTERACTIVE_COMMENTS
	function> #echo test
	function> }
	% foo
	foo:2: command not found: #echo

	% unsetopt INTERACTIVE_COMMENTS
	% (
	subsh> setopt INTERACTIVE_COMMENTS
	subsh> #echo test
	subsh> )
	zsh: command not found: #echo

	% unsetopt INTERACTIVE_COMMENTS               
	% if :; then
	then> setopt INTERACTIVE_COMMENTS
	then> #echo test
	then> fi
	zsh: command not found: #echo


> Why doesn't setting interactive_comments within an anonymous function work?

Someone please correct me if I'm mistaken, but I believe that:

- Comment removal is performed during parsing.  Thus, setting and
  unsetting INTERACTIVE_COMMENTS changes how parsing is done.
- A complex command is parsed *in its entirety* before *any* of its
  commands are executed.  Thus, a command within a complex command
  cannot affect how the rest of the complex command is parsed.

Therefore, a complex command cannot enable or disable comments
within itself.  It can affect subsequent commands, though:

	% unsetopt INTERACTIVE_COMMENTS
	% {
	cursh> setopt INTERACTIVE_COMMENTS
	cursh> #echo test
	cursh> }
	zsh: command not found: #echo
	% #echo test
	%

I don't know whether any of this is explicitly described in the
documentation.

-- 
vq




Messages sorted by: Reverse Date, Date, Thread, Author