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

Idiom for booleans



Hi! I'm trying to figure out if the $+ expansion, as in ${+param}, is
worth making use of. What idiom do you use for it? I could see it as a
boolean for testing flags. As in:

  % verbosity=high myscript.zsh
  ...
  (( $+verbosity )) && print chatty
  chatty

  % unset verbosity  # uh-oh
  % (( $+verbosity )) && print chatty  # should this work?
  ((: command not found

I did see this idiom used in the Zsh Guide
(http://zsh.sourceforge.net/Guide/zshguide03.html as: (( OPTIND > 1 ))
&& shift $(( OPTIND - 1 )) ) -- is that a bug?

I suppose it boils down to understanding (explanation welcome here):

  % (( 0 ))
  ((: command not found
  % (( 1 ))  # ok

Wrapping in "if" makes it work too, but just feels tedious:

  % if (( 0 )); then print i say nothing; fi

----

I could make the boolean work by adding a fall-thru:

  % (( $+verbosity )) && print chatty || :

but that feels rotten. Or I could use:

  % [[ $+verbosity == 1 ]] && print chatty

but that doesn't seem any better than the more common and simpler:

  % [[ -n $verbosity ]] && print chatty

So is there some concise idiomatic way of making use of $+ ? Or is
this last line the better way to make such tests?

--
twitter:@Membean  |  email:Micah@xxxxxxxxxxx  |  http://Membean.com
Remember your words with Membean. Three free days of learning! GO SIGN UP.



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