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 E129C2C4F7 for ; Tue, 20 Feb 2024 23:31:46 +0100 (CET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1708468306; b=stIewMOBgFrCHr2OMj4AXpzN5/5jkgAHjEt9VzV/00fqyKJVLA1CmnD+VNk9F5Iv8xjZV7C5C2 BjQfnBLbdnCC9Si/XntSwYeQ1qNXjU6x9d64iehiQ4NO4ShAfgtFBr37OxFtlqFhOe9WhrS1SK QpCRcFHFkLanCOd94AiGAZdGZu43Xb1P+TnPRwBxjXktkHBcY6KVO0y1+vz3C3B2CkZfG03ViB +d8OIePJMQtaOUf0YsO+DfdIkNxO1+yM4IaOMcZ8tleNaYJlNWXoZHqrR3SneNzQlAUG0XcwwY w8XylPzfdL0xqSfexBNxFUo+6S+gOjLeApbomqnoZ9Byrw==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-ej1-f42.google.com) smtp.remote-ip=209.85.218.42; dkim=pass header.d=brasslantern-com.20230601.gappssmtp.com header.s=20230601 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1708468306; bh=vowZjm8+Uar5X0emKdSzVJqg8IopV22ZdlhwSL4+nAQ=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Transfer-Encoding:Content-Type:To:Subject:Message-ID: Date:From:In-Reply-To:References:MIME-Version:DKIM-Signature:DKIM-Signature; b=F8p/nW8zCmI2hUYqRm9i2gq9O/H8MPY8qyH6CTrc/7ui3F4oDLslEWEiYDDYBW+qblngVYwOF9 JBf2V49pa0Cgw9RqVjM9suow6jFs1XTJPG9zFgtrng1nray5vQVJiERGeaiGHhQoKj+q/p/8CE nPQhgclIGvMpzdVdrfHgG/iApUEh0ISLWlq1RDxEMJRr9bGpLBADl4FO7Yn9qJBYhFQJRznQoi 92qYDNrTrbYqMxYrLsBY+zk6rM/qxPKXCARJvBwtgO6zesYdPiKev1tusoGYwvbZD6JXmYmFLX 4e5OYY6ag2QeGOZHXFyX5fgSFapjRWDb8JvMfpFfYrwz4A==; 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:Content-Transfer-Encoding: Content-Type:To:Subject:Message-ID:Date:From:In-Reply-To:References: MIME-Version:Reply-To:Cc:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=XTN0HHcaY4Rja/VN1Rbz+MEozd+MBeZFeiSu28+MNJk=; b=RkwHdIf+nfuA5tABOl0FseuQRz uYs7bvB+p45/nnFi4l95o5wJYOkxO83zZ14lB+ZWqD8Qs9R06sZewlbIBhR7MA3PW+Yc8SGBPupek jFuM2/O5CHB3AoR5s/FX6JPhWkZx1MiJtBx7HFKGZu8HY1NbLkhxGSVY6WG2MLpUbjB5V17/vqxoZ qGD+3F9Ynk8Wfn7oU8p449LExQLsv9ftnGJdnONYti6T7D2yOGrmRo9tK+1rfesSOfVy18FC+ELhW dWGu/bh0TKRtqEEvb9UWdrv2N06RZU+sToffD9dUYpDm14fuHqMfvVUWx/HfnaCyljB6TOmv3SIxZ 1njW6tGA==; Received: by zero.zsh.org with local id 1rcYeQ-000BJ0-5M; Tue, 20 Feb 2024 22:31:46 +0000 Authentication-Results: zsh.org; iprev=pass (mail-ej1-f42.google.com) smtp.remote-ip=209.85.218.42; dkim=pass header.d=brasslantern-com.20230601.gappssmtp.com header.s=20230601 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none Received: from mail-ej1-f42.google.com ([209.85.218.42]:57799) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1rcYdp-000Az0-4B; Tue, 20 Feb 2024 22:31:10 +0000 Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-a3e82664d53so358144466b.3 for ; Tue, 20 Feb 2024 14:31:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20230601.gappssmtp.com; s=20230601; t=1708468268; x=1709073068; darn=zsh.org; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=XTN0HHcaY4Rja/VN1Rbz+MEozd+MBeZFeiSu28+MNJk=; b=PGn8DSG+Iw+3roagjEF7Qv5jOkoVXAPiv+TOmjHNYtR/FFtOT9/FWUDRRCJ2wMRq5s a5QlpmNrVFgY8s2YabY+wDkTh4CTreYHoDlOFMMOgMCEd0xUOVWZa58BsZe1arQWE/wz fmkW6DTlcBn6Kztxg69+ceSI1gU1UYUrbAAMfY4uP3jsXvSfxf3reozDDiaQ/6gQPye1 a2PhJMbCOTlMmJYj4gRoHvBwqYoPGZ29nKdIkFtu98lR/vcTcCzF6sPXYy48UaTr/HOQ fa8mHrNUVbEVWqW0LjNp8AekQCktDkZEWddwiZ4/OBgM2QeXbLGbbxKQP/TUavb1xDEc drVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708468268; x=1709073068; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XTN0HHcaY4Rja/VN1Rbz+MEozd+MBeZFeiSu28+MNJk=; b=Dhq3fWz2PtY6MRRI8+fMq7/9rzFOoJeKmKxhLska371WfkeXDZXewRazmpj37jeshs Wg2TR3b5aQZVcS5//Jk1AcjnDMjHvemSp0j7REdlOco4rgHlsQsPnZXgprLv+E3WyPQ2 0ETAIF4vVl+sIHG3kLm19GFcasDxwFIRH7rR34vowaj/7Q3+pueFT5qPPD30eugsYDJU by+/vqfE0ZH3ebx8o4r/GU48nlpdvaBZi1pITqf81Lgx/Uu0SQBg4H4HXgJZgunRmNSz 9fxojjgFk81NSyXZS4h2YluDmUHW01CNwCRIZ2vHsTCgNxt4MS6uTtDCxywbdXr6wsiX SpPQ== X-Gm-Message-State: AOJu0YyJaPadUaNrXNuQhE9yRy0QA6Lf2W8JUzkOwCoSU5PfXrAdPxBe R7Xv2VAFgcWpALwy0QvUN5URZG1Uu+BXGNoxt6PAybeYfA5Ecgv9d8sPnBp3V0viiPyYek0xPhp 6sCL+E/XhqMNKbb4gaI/bZRZLGGQ9FUBNbtSnvW5qCqOEKZj+gw== X-Google-Smtp-Source: AGHT+IHVebJ67bXwUgkCwmiFFPvSScqAWHKcr6AsIifDxnCS5/LJCEngZgRQyoBhwhet+75F+3V2BxCQSV9sPKSHTHg= X-Received: by 2002:a17:906:a19a:b0:a3d:2a52:380f with SMTP id s26-20020a170906a19a00b00a3d2a52380fmr10963601ejy.72.1708468268074; Tue, 20 Feb 2024 14:31:08 -0800 (PST) MIME-Version: 1.0 References: <12608-1675903622.800470@Xj82.e3y1.svhG> <66045-1675975796.128039@FBF_.0yMO.Y8fk> <40726-1676098925.110777@U2kb.d0Pd.I9ml> <20240211070042.4j37hkgjjn3dfjqd@chazelas.org> <20240220210553.g6imt3op6zahz4pa@chazelas.org> In-Reply-To: <20240220210553.g6imt3op6zahz4pa@chazelas.org> From: Bart Schaefer Date: Tue, 20 Feb 2024 14:30:56 -0800 Message-ID: Subject: Re: Up-scope named references, vs. ksh To: Zsh hackers list Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Seq: 52565 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: On Tue, Feb 20, 2024 at 1:05=E2=80=AFPM Stephane Chazelas wrote: > > 2024-02-17 19:26:07 -0800, Bart Schaefer: > [...] > > A) Src/zsh -c 'function f { typeset -n ref; ref=3D$1; typeset var=3Dfo= o; > > ref=3DX; 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 comman= d > ksh93> [...] For instance, typeset -n var; var=3D$1 > ksh93> won't cross that barrier And yet, after replacing ksh2020 with Version AJM 93u+ 2012-08-01 (I had already done this on a different workstation but had forgotten about it on the one where I tested): ksh -c 'function f { typeset -n ref; ref=3D$1; typeset var=3Dfoo; ref=3DX; 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. With 93u+ "typeset -n ref=3D;" produces ksh[2]: f[1]: typeset: : invalid variable name rather than crashing. > Note that ksh93 is special in that it does static scoping. > > I suppose zsh's private variables are similar to that. Sort of ... private variables are never accessible in another scope, even with namerefs. > [...] > > Given that it's not possible to fix part C for zsh, and zsh agrees > > with ksh on part B and with mksh on B and C, is it worth making an > > effort to fix Stephane's original example along with part A ? > [...] > > Seems to me if we have to work around it by namespacing > variables or use argv like in functions/regexp-replace, that > defeats the purpose of those namerefs. You don't have to use namespacing, you just have to be sure that either the caller has declared the parameter that it wants referenced, or that the called function does the "typeset -g $1" preliminary (and follows the doc recommendation about doing the "typeset -n" early). There's nothing really to be done about the other cases, and particularly not about specifying the (dynamic) scope of the referent. > 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. Or maybe add an option to "typeset" to turn this on or off, though I'm not sure the existing function call framework provides all the needed info. > > The potential for confusion here seems large. If I do > > > > typeset -n up=3D$1 > > typeset var=3Dfoo > > for up in $2 $3 $4 $5; do ... > > > > what scope am I applying > > I find that special behaviour of "for" quite surprising. 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. Mixing namerefs with dynamic scoping does have a number of potential gotcha= s. > I wasn't aware ksh93 and bash did it as well. If they didn't, I wouldn't have implemented it. > Seems buggy in ksh93: Not all that surprising given the potential confusions. > $ mksh -c 'a=3D1 b=3D2; f() { typeset name; for name do typeset -n v=3D$n= ame; echo "$name=3D$v"; done; }; f a b' > a=3D1 > b=3D2 > > Won't work for "f name". There's probably a way to do it with "f name a b" + shift, but I'm not going to work it out. > I can see how it can still be difficult to do without > namespacing. For instance having a function that does option > processing =C3=A0 la print -v var would have to do: zparseopts and/or a declared assoc can get you a long way in this sort of example while minimizing the opportunity for clashes.