From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2751 invoked from network); 25 Aug 2008 00:13:06 -0000 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.5 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 25 Aug 2008 00:13:06 -0000 Received-SPF: none (ns1.primenet.com.au: domain at sunsite.dk does not designate permitted sender hosts) Received: (qmail 60661 invoked from network); 25 Aug 2008 00:13:01 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 25 Aug 2008 00:13:01 -0000 Received: (qmail 25390 invoked by alias); 25 Aug 2008 00:12:50 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 25527 Received: (qmail 25372 invoked from network); 25 Aug 2008 00:12:48 -0000 Received: from bifrost.dotsrc.org (130.225.254.106) by sunsite.dk with SMTP; 25 Aug 2008 00:12:48 -0000 Received: from wa-out-1112.google.com (wa-out-1112.google.com [209.85.146.178]) by bifrost.dotsrc.org (Postfix) with ESMTP id 15AA48030846 for ; Mon, 25 Aug 2008 02:12:43 +0200 (CEST) Received: by wa-out-1112.google.com with SMTP id v27so717641wah.21 for ; Sun, 24 Aug 2008 17:12:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=2jJRAsUeVekmWjVIkiYjbzPbRt/OtUnj+y5nO8O2Q70=; b=X1+mEI403VQOy61sqNU6y7Za/cczuMLkzdYc/nxhF33vIcfepbVlYtXtrHxNxYRAb3 JAeei4CLnfW50kxC4Qv95RQqtgozvFq4ceXNi0GUmOw8yuR1Z9KuN/4SFRzA0t3l9a0b v23bQG4NbM13tHttOoX5OjHAaC45Zp+pvwaBE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=ieFzS8kMADitCnTAoYoWxkh9YzPCrHu1sb1qFHPVSPA+/y9PTMa7uX08lw/zbXoIqp juwzeb9zjR5EzUYBTsBhn7HzmLq7cWeE9oncX0U217nlsYxIJT+LXrvLMJCOKcCfRB67 XHbGq2Z6eG6tW1csoS832eAfJsEeIYkstrh3s= Received: by 10.114.210.2 with SMTP id i2mr2900749wag.18.1219623161895; Sun, 24 Aug 2008 17:12:41 -0700 (PDT) Received: by 10.114.159.2 with HTTP; Sun, 24 Aug 2008 17:12:41 -0700 (PDT) Message-ID: <6cd6de210808241712o2c6d4af3k2dcdf0b7721f79fe@mail.gmail.com> Date: Sun, 24 Aug 2008 20:12:41 -0400 From: "Rocky Bernstein" To: "Zsh hackers list" Subject: Re: Weird error message when using bash or ksh form of array initialization In-Reply-To: <080824133218.ZM16802@torch.brasslantern.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <6cd6de210808240905s40c8c86ch4766b711e87ffde5@mail.gmail.com> <080824133218.ZM16802@torch.brasslantern.com> X-Virus-Scanned: ClamAV 0.92.1/8082/Sun Aug 24 18:40:03 2008 on bifrost X-Virus-Status: Clean Thanks folks for the explanation of what's happening. It kind of strikes me as not a good language design when entering similar valid constructs from a couple of other POSIX-compatible shells causes such a damaging misinterpretation to take place. You say: typeset -a foo=() typeset -a sources defines "typeset", "-a" and "foo=" as functions? Wow! I'd be hard pressed to find many would would find that an advantage. It reminds me of this example sometimes mentioned in the context of Fortran. If one writes: DO 10 I=1.5 instead of DO 10 I=1,5 both are valid but mean very different things. The first sets variable DO10I to 1.5 in one of the weird, but acceptable, ways. The second is probably a much more common Fortran statement to start a loop. I mention this because when you read about this with respect to Fortran, it is not generally given in the context how this distinction is a interesting thing that programmer should be aware of, or what a great language Fortran is because it allows such things to be confused. Rather it is given as a consequence of the weakness of some language-design decisions. In the context of zsh, we have one of the zsh function forms: word ... () [ term ] command competing (and winning) with this erroneous statement: typeset -a xx=() Given that the relevant function form used is zsh specific, it doesn't strike me that the convenience this form allows outweighs the damage of the consequences of its inadvertent misuse above. Is it really that helpful to allow define several functions with the same body? If so, there are other ways to do this. Here are some simple solutions to make what's happening clearer, reduce confusion and give a better error message. * change "command" to "compound command" above. * disallow the shared-body multiple function definition, i.e. remove ... above * disallow that specific kind of function in ksh and bash emulation form since it isn't POSIX 1003.1 anyway. Comments in line, but I think they say pretty much the same as above. On Sun, Aug 24, 2008 at 4:32 PM, Bart Schaefer wrote: > On Aug 24, 12:05pm, Rocky Bernstein wrote: > } > } When I run this (erroneous?) program: > } > } typeset -a fd=() > > Zsh does not support that syntax; I realize that. And IMHO I think it should support it when bash or ksh emulation is in force ;-). But more troublesome is the fact that it gives an error message that is probably going to be a bit puzzling to most people who know ksh and bash, but zsh less so. > typeset is a command, not a keyword, > so its arguments are parsed like normal shell words, not like parameter > assignments. Um. Okay. "typeset" is not a reserved word in POSIX shell, bash or ksh either. That in of itself is not a big problem. It is when it is combined with this zsh-specific function definition form: word ... () [ term ] command that we get bad karma. The above is not a POSIX 1003.1 function form. The following is: fname() compound-command[io-redirect ...] ^^^^^^^^^^^^^ So changing "command" above to "compound command" would be enough to give a more meaningful error message. There are other possibilities. > > As Mikael pointed out, the above syntax defines three functions, named > "typeset", "-a", and "fd=", because the shell word "()" is a token that > denotes that a function name precedes it and a function body follows. > > Quoting it as 'fd=()' like Mikael suggested would prevent the function > definition interpretation, but produces the error "can't assign initial > value for array". I think it interesting that like zsh, Ruby does in fact does allow function (or more appropriately, methods) to have a '=' suffix. But where Ruby differs is that this allows for a semantic simplification by allowing one to write something that looks like an assignment statement when it is in fact a method call (usually setting an instance variable). Furthermore, there is syntactic sugar to allows spaces around the equals. So in contrast, zsh allows function definitions with = suffixes, but other parts of the zsh language work to not allow it the benefits it might otherwise have and that, for example, that Ruby exploits.