From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: from zero.zsh.org (zero.zsh.org [IPv6:2a02:898:31:0:48:4558:7a:7368]) by inbox.vuxu.org (Postfix) with ESMTP id 33C072370D for ; Wed, 21 Feb 2024 21:12:30 +0100 (CET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1708546350; b=TMM8rzRjkry+7KbQaxfJLatNZBqT/567X5YFqH9D3GYMbUFgqQwFPIXrvTzfi3tBfUcJC9znbM yoi4uA2KuWDHtRT1ZdgGQC9GjeeM98KbDoUqMI2wZM2Pm89hl1GluTmBifgr7jdKGOh3y1BqIG NOO8c8UAwD3wlCMn8/lnBMDfvBELRCZAD5WnErJBuCbsAsLSO+RG+OaV8vabciDWv9vvqDxmB+ CwkLjDyierN/cdrm9hfvWqVS/gf5LOZ3nYUw7Oqa77+s50+VEK9PfPHvsCHj5LMsDxaYxaRY8q ZL7GNhTePlnj3IEuPy0ckSLQIOuuyR1aRRP7XzCJm2hACw==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (relay2-d.mail.gandi.net) smtp.remote-ip=217.70.183.194; dmarc=none header.from=chazelas.org; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1708546350; bh=jTlVd+EbXB79XRor2bRhh3Py8nXNQGt58DmtcVoD/uo=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:DKIM-Signature; b=JRNAqk/O7RNpJyeJ7Rb7trzov7EWtm6D7IAHI1+EZMT/b0cYU/sjIVGa86h51UX9NXboptAxOG lzWYxXg0VimbN0XvygxERsHH9AWcYtA7arSwHCPImR9wzBCBMyVRFIeEvPzm1/RYl4SDUIYk99 3UpZ36dniLHP5Lg32HgKHZzm85sFzLtftkciN3tAKNw1YjtLzXdEszI0yLExhqnIsCTYpnnAn/ SkechFFr+iD+VmBQPqWXOWLTc4NXfE+RkXxc8y5hzZr6RjwmBqtvFfDRv6TOYsKqhtSECQsPkb 2u1wVGZPwhTzQ4G4w+S6BNGJNnM2LdE2o+IENdIhBvedag==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:In-Reply-To:Content-Transfer-Encoding :Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID; bh=FIjOF+q5o7Rzd4aiPt/G3875APEQoQui4uQsMcJJv6k=; b=cDWw682cc2trLxHjhPAgSlHaI0 +94MQST3dDzqX6CQU26tnlKdBqlAd7bzlY6JBQfabolkFU6qxRr2U7f+efonDJWwFVwZc5+ThRTM2 E+m3mhbzhx+GaOzi49unhzDeqyGhPlsgo94sp8DNVMbaui1ETyxb4VXrbZxMNzy8VyP/BKvLJg00D xbSfAhACaHGeMfdx2J5GUuf6ZiHctfHwdG3r9DYzYBXmRQFJ0x5lglYUMAnF1gp2yNUuKgXHEbskR 3LzgIjVLxJvI/qF8lFjvZcaKsEioUY9AhccpGz4+m71YXgXOc3Iefmw2pLs2SI/pHA/b//4a2qF52 WmgU8Q7A==; Received: by zero.zsh.org with local id 1rcsxC-000MGh-8Q; Wed, 21 Feb 2024 20:12:30 +0000 Authentication-Results: zsh.org; iprev=pass (relay2-d.mail.gandi.net) smtp.remote-ip=217.70.183.194; dmarc=none header.from=chazelas.org; arc=none Received: from relay2-d.mail.gandi.net ([217.70.183.194]:56315) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1rcswy-000Lzm-Q7; Wed, 21 Feb 2024 20:12:18 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id E367F40003; Wed, 21 Feb 2024 20:12:15 +0000 (UTC) Date: Wed, 21 Feb 2024 20:12:15 +0000 From: Stephane Chazelas To: Bart Schaefer Cc: Zsh hackers list Subject: Re: Up-scope named references, vs. ksh Message-ID: <20240221201215.anpjcfav6na55gg6@chazelas.org> Mail-Followup-To: Bart Schaefer , Zsh hackers list References: <66045-1675975796.128039@FBF_.0yMO.Y8fk> <40726-1676098925.110777@U2kb.d0Pd.I9ml> <20240211070042.4j37hkgjjn3dfjqd@chazelas.org> <20240220210553.g6imt3op6zahz4pa@chazelas.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-GND-Sasl: stephane@chazelas.org X-Seq: 52571 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: 2024-02-20 14:30:56 -0800, Bart Schaefer: > On Tue, Feb 20, 2024 at 1:05 PM Stephane Chazelas wrote: > > > > 2024-02-17 19:26:07 -0800, Bart Schaefer: > > [...] > > > A) Src/zsh -c 'function f { typeset -n ref; ref=$1; typeset var=foo; > > > ref=X; echo "$ref ${(!)ref} $var"; }; f var; echo "$var"' > > > > Note that the ksh93 man page mentions: > > > > ksh93> Note that, for this to work, the positional parameter must be > > ksh93> assigned directly to the nameref as part of the declaration command > > ksh93> [...] For instance, typeset -n var; var=$1 > > ksh93> won't cross that barrier > > And yet, after replacing ksh2020 with Version AJM 93u+ 2012-08-01 With recent maintained versions with all the bug fixes, it would look something like: $ ./arch/linux.i386-64/bin/ksh -c 'echo ${.sh.version}' Version AJM 93u+m/1.1.0-alpha+b8e3d2a4 2024-02-17 The one on Ubuntu 22.04 has: $ ksh -c 'echo ${.sh.version}' Version AJM 93u+m/1.0.0-beta.2 2021-12-17 93u+ 2012-08-01 would be the one from AT&T without the later bug fixes. > ksh -c 'function f { typeset -n ref; ref=$1; typeset var=foo; > ref=X; echo "$ref ${!ref} $var"; }; f var; echo "$var"' > X var foo > X > > So it sure looks like the barrier was crossed in a case where the doc > stated it would not be. But in that case, you're referencing a variable from the global scope, not the local scope from a parent function. [...] > > The only remaining problem is when the refered variable > > is not set/declared. > > It IS possible to make that "typeset -g" implicit, but that has other > potential effects such as creating an (unset) parameter in global > scope (triggering WARN_CREATE_GLOBAL?). If the parameter already has > been declared in a calling function scope, then even with the -g, the > nameref will point to that one, even if it's more than one level up > the call chain. Which ought to be what you expect from dynamic > scoping. > > > Contrary to mksh or bash, it alread gets this right: > > Yes, as soon as an existing parameter is found the nameref records > what scope contains that parameter and looks for it there when > (de)referenced. The glitchy bit is when there is no existing > parameter from which to obtain a scope. I'm reluctant to try to make > this too magical, which is why I lean toward asking function authors > to explicitly do the "typeset -g $1" when that's what they mean. > Could certainly add this to the doc. [...] I have no notion of how zsh variables are implemented so I'm probably talking rubish and it's hard to think this whole thing through, but it seems to me that for a nameref variable, upon typeset -n ref=var, zsh should record at that point what scope or level of the stack the var is to be found and keep that record for the duration of the scope where that nameref is defined or until the next typeset -n ref=anything in the same scope. Like: - if var is found (declared) in the current scope, record that it's that of that scope that it is to get the value of or set or unset (and reset again thereafter) - if not, repeat for parent scope - if not found at all, record that it's to be at the global scope. Like in: f() { typeset a; unset a; g; echo "$a"; } g() { h; } h() { typeset b=x typeset -n r1=a r2=b r3=c typeset a=foo c=bar r1=x r2=y r3=z unset r{1..3} r1=x r2=y r3=z } f r1=x sets the $a of f, r2=y sets the $b of h, and r3=z sets a global $c both times. [...] > So did I. I await with gritted teeth the first time that Ray forgets > to declare his loop variable with a nameref of the same name in scope. [...] Yes, difficult decision to choose between - cleaner design, align with mksh - align with original design in ksh93 and bash which already copied it (well, that part). -- Stephane