zsh-users
 help / color / mirror / code / Atom feed
* Case statement with ";|" [not implemented]
@ 2007-01-18 19:53 Gerald Lai
  2007-01-19  4:52 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Gerald Lai @ 2007-01-18 19:53 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1216 bytes --]

Hello all :)

I'm new to the mailing list.

I'm trying to convert the (simplified) code below to a case statement
structure:
===
  if [[ $mycase -eq 0 ]]; then
      echo "mycase is 0"
  else
      if [[ $mycase -eq 1 ]]; then
          echo "mycase is 1"
      else
          echo "mycase is 2"
      fi
      echo "mycase is 1 or 2"
  fi
===

And this is what I got:
===
  case $mycase in
      0)
          echo "mycase is 0"
          ;;

      1)
          echo "mycase is 1"
          ;;

      2)
          echo "mycase is 2"
  esac
  if [[ $mycase -gt 0 ]] echo "mycase is 1 or 2"
===

My question is, shouldn't there be a way to do this:
===
  case $mycase in
      0)
          echo "mycase is 0"
          ;;

      1)
          echo "mycase is 1"
          ;|

      2)
          echo "mycase is 2"
          ;|

      *)
          echo "mycase is 1 or 2"
  esac
===

Note that ";|" provides an OR-like separator (as opposed to the AND-like
";&" separator) that goes through the case statements. However, unlike ";&",
it checks $mycase against 1) and 2) instead of blindly executing case
statements 1 & 2.

Wouldn't this be a good addition to ZSH? It certainly seems more useful than
";&".
--
Gerald Lai

[-- Attachment #2: Type: text/html, Size: 2558 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Case statement with ";|" [not implemented]
  2007-01-18 19:53 Case statement with ";|" [not implemented] Gerald Lai
@ 2007-01-19  4:52 ` Bart Schaefer
  2007-01-19 12:01   ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2007-01-19  4:52 UTC (permalink / raw)
  To: zsh-users

On Jan 18, 11:53am, Gerald Lai wrote:
}
} My question is, shouldn't there be a way to do this:
} ===
}   case $mycase in
}       0)
}           echo "mycase is 0"
}           ;;
} 
}       1)
}           echo "mycase is 1"
}           ;|
} 
}       2)
}           echo "mycase is 2"
}           ;|
} 
}       *)
}           echo "mycase is 1 or 2"
}   esac
} ===
} Note that ";|" provides an OR-like separator (as opposed to the AND-like
} ";&" separator)

This analogy doesn't work.  Semantically, the test for whether $mycase
matches any one of the labels occurs once at the top of the statement.
Control then jumps into the body as if with a GOTO, and thereafter all
you're able to change with ;& is how early you exit from the body.  It
isn't anything like AND / OR.

By comparison, your proposed ;| requires the interpreter to reproduce
the pattern match test at every label, which implies storing the result
of expanding $mycase, etc.  Most languages don't allow one to hide quite
that much complexity behind syntactic sugar.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Case statement with ";|" [not implemented]
  2007-01-19  4:52 ` Bart Schaefer
@ 2007-01-19 12:01   ` Peter Stephenson
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2007-01-19 12:01 UTC (permalink / raw)
  To: zsh-users

Bart Schaefer wrote:
> } Note that ";|" provides an OR-like separator (as opposed to the AND-like
> } ";&" separator)
> 
> This analogy doesn't work.  Semantically, the test for whether $mycase
> matches any one of the labels occurs once at the top of the statement.
> Control then jumps into the body as if with a GOTO, and thereafter all
> you're able to change with ;& is how early you exit from the body.  It
> isn't anything like AND / OR.
> 
> By comparison, your proposed ;| requires the interpreter to reproduce
> the pattern match test at every label, which implies storing the result
> of expanding $mycase, etc.  Most languages don't allow one to hide quite
> that much complexity behind syntactic sugar.

I think we simply have to apply the restriction that the word in the
case statement is expanded only once at the start of the case statement.
I don't see that's either unnatural or confusing.  With this it looks
like it ought to be quite straightforward, possible even trivial, to
implement.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php

To get further information regarding CSR, please visit our Investor Relations page at http://ir.csr.com/csr/about/overview


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-01-19 12:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-18 19:53 Case statement with ";|" [not implemented] Gerald Lai
2007-01-19  4:52 ` Bart Schaefer
2007-01-19 12:01   ` Peter Stephenson

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).