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 EF3762BAC5 for ; Sun, 3 Mar 2024 23:58:53 +0100 (CET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1709506733; b=LfF5dTd/RuEPWqEhwUkYocxuBQ6+zj1wkCIU9bDxYqksAanVhwalMSTHCBsFKyQjt9myAccTRh tpeJQVbrUFX4keHkHawbKxkrfq+RpVE7mK/GKxzGcN2RDAL+QmHknRo9PHzy3zpq0NAcyDInSV 4eBuaiLyAd5180cdh24JL4K+/MJt31t5ed9eO8fNKZfZnop9dgGcWEq3xblPzzX678qXlizwOn SlVHb4EPy5o2Z9qGNqDFVY3tG7mzOrKkmbGgB/LU+tqgKn1HxfjNWAE0Kel1GXbdUJ6nYX8l3W KDJecorp/BNAeG/T2AgEHNS5ln924+X3dTmEcyakn85NaA==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-ej1-f45.google.com) smtp.remote-ip=209.85.218.45; 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=1709506733; bh=Axi9kVIkgXgOJbwFiZ/P50GQ/5FCzFu5QoDh9WT8OJQ=; 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=GMtp19V9SAA5l7udoyhiqiTxUGrT7nZHDXud2WKkmeeYESfvBAPIeToxUPsnR6zi+vNz9HC5AM MaF/MRudOLE7LexKH6jOCSY0wBrFSwNG2TXvBzu1lxoCc2VOh2gUW3iKoCTJLM8HgwJW5lYuU3 BJKAUaA0+gxMHHr5AGUboJT45VnBQGB5HEGyfCFRHJ+TKe44W9zCw8/GVOvlRdPyXfJnNIWHmm JSFwge1ebqSkPz6rB1DwwrKMduAW+nHGDw5nSHI5GBlD82HAFF8cBXNlCfoPLdVTguGA3qFnsu oheWusVffCUFoBtTxM2mRiAknW+F7dW+FNpIWPh80UlgEQ==; 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=I2gedhJY0pBDrDvF1bzRamlFbPw1o2g9cO28nFLmCvs=; b=Zi9cuhq7Y6opi9PdbEJ4d50hoh MLZqpIeCqMZ8Y92k6xqkbdKfVk28qnIuuqf520OhPjVSjoQGqrz9LAQAPsYDjso3bt3ZkFe5Gm5oh 9SDNbCzdjNuXuALdqnyyePmxRk66nmkSMXTjSr/+V60QEkr2IBlkGllPsf3lEkPmkwgea0lnMu3AI VZVMdHqf//dxp294FiW4OqZNGzg/ozSjGUVx2q+tWKimwn1wwbUJD4gaXO5D+LMqn4VMlmuEm8V8P 4JSneWifPhxkj/7u6QAhnBjZbKm9nZ+9loPFj75k9dPMUpXn1N5CeUEyXrnMiKQey3593unEgCYUo 8bzMRbIQ==; Received: by zero.zsh.org with local id 1rgunE-0004xK-AX; Sun, 03 Mar 2024 22:58:52 +0000 Authentication-Results: zsh.org; iprev=pass (mail-ej1-f45.google.com) smtp.remote-ip=209.85.218.45; 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-f45.google.com ([209.85.218.45]:45182) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1rgume-0004cZ-N3; Sun, 03 Mar 2024 22:58:18 +0000 Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-a44cdb2d3a6so182306566b.2 for ; Sun, 03 Mar 2024 14:58:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20230601.gappssmtp.com; s=20230601; t=1709506696; x=1710111496; 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=I2gedhJY0pBDrDvF1bzRamlFbPw1o2g9cO28nFLmCvs=; b=J8ze1dL9vMloVhxPU5mdqiDEyui2TDTiwevdwpFxRL4a4gSI+lmooJdHX/q2ty7jjD Y9ZP6yXFnIIepR2akwqS2B8H+KsyGbvkiyVflZ+POkFroSJq0Vowoll3Cxl6ftojmaCa E+55Ch1w/LKvG3zHVQz8dixdPgPEU5TSQN1iWU5wnU3R9QT73JH8Y/OftUznnbkJf/f0 2cc5RXoczbfOAg+ramwyUhpTZDi7XWELuG3djFAA+WBadyzS00PdtWPz8kAYLGoQ7Npr 1D75H0Y3LIxs4P3JENatFI6TsBjUi8VuBw9hwPoN8JDRFUswBmgfy7b689JXiTYC25/9 xjDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709506696; x=1710111496; 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=I2gedhJY0pBDrDvF1bzRamlFbPw1o2g9cO28nFLmCvs=; b=CDsOw6GsBf9VP0H1A2LnYMyWbg9orlZZMTXNsYUMhzgE6G12LI9OBB+pJbZuqpGwMW 7iPFYZCCxgp4y+mk6qsewBvs5OONVmMm8FrJSxIwLJQQJOvpcOpHCNscNdSwKhzUOJTE M8mveRSLQWLOnggbgqr7COgNj0qYCKcUzPAtdgmorgiS/v0rnFnrl0AXjozOJ69S4F8K /seOKbBlbsTdYCALS2+ozpyp+H0ksh7tlmJ3t0swlLVbGyVjNnBOOjXAYIAhjXKuctgg MdmG2Uap7vWA5zPGs0HrbEfHculFYemMZnNRLamvirCFj1Sk45ufB2qEjteo4WSzMIK2 JZPw== X-Gm-Message-State: AOJu0YxodDWsyAwvookojNq4ZKGtPzxw7KBGya6O7gQkTyHKgqdrmjx8 cX58fhUsuBfbL/fI0Z7GnZmbgBUG6eRXWaS8xqxFuW9ffEk7anvB4nkPh6P4zCLBlfPI2cscvt/ N7glmbzPsHfR2wc7vHHAl65yqOAN7NYairnTnQu3Yrgdk6aE= X-Google-Smtp-Source: AGHT+IE1Kr5S3YseOAdnR67/y/tJbVKYxEje5tvzC+yOIa2FgOWIXsiVNwIYkl7QHmmqVo54PKgDDaCjeW2nQcBMRVk= X-Received: by 2002:a17:906:81d4:b0:a45:3792:fac8 with SMTP id e20-20020a17090681d400b00a453792fac8mr993541ejx.25.1709506695812; Sun, 03 Mar 2024 14:58:15 -0800 (PST) MIME-Version: 1.0 References: <20240220210553.g6imt3op6zahz4pa@chazelas.org> <20240221201215.anpjcfav6na55gg6@chazelas.org> <20240301182238.tpyajwblbam5bxw7@chazelas.org> <20240303134413.74c7trikf73g5kjy@chazelas.org> <20240303202756.7axmzy6gkohza2ra@chazelas.org> In-Reply-To: <20240303202756.7axmzy6gkohza2ra@chazelas.org> From: Bart Schaefer Date: Sun, 3 Mar 2024 14:58:04 -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: 52656 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 Sun, Mar 3, 2024 at 12:27=E2=80=AFPM Stephane Chazelas wrote: > > 2024-03-03 11:04:03 -0800, Bart Schaefer: > > > > If the parameter is magically created "in advance" (at the point of > > "typeset -n") then it only works for global scalars > > Why would we need it for any other scope? If the parameter is > not found in any scope, then it must be created in global scope. > > That's what "read var" does. The word "scalar" is just as important in that sentence as the word "global" -- "read" and "zstat" can specify whether the variable is scalar or array, you can't do that with a nameref. Furthermore, if there's a local in scope at the point where "read var" is called, it assigns to the local, even if the local is unset. The scope is determined at the point of assignment. Locals consume the space allocated in the parameter table for the corresponding name. This is a longstanding, conscious decision in the parameter implementation, I even quoted a code comment to that effect in the thread about unsetting tied parameters. With -u, a nameref can skip over one local, but it can't "make room" for a global that didn't already exist. I've tried several different ways to explain this, so I'm just going to ignore this going forward. > Many if not most of the usages of > namerefs I've come across were to implement "read"-like commands > that return or can return something in a variable whose name is given by = the > caller (and that can create the variable if needed). There's nothing in the current implementation that prevents you from doing that if you choose your local names appropriately (and if nothing above you in the call chain hasn't already co-opted the name). > Where an approximation of what I'm suggesting is implemented in > zsh seems to work fine (with your patch applied). But that approach doesn't work in the general case! If you move typeset value=3D$2 to above the [[ -v $1 ]], then it either bombs, or assigns to the "wrong" $value. There's no way to magically get this right in the internal implementation, it has to be handled in shell code. > It segfaults with zsh -x though I'll have a look at that later, thanks. > > If a function wants to be able to actually add things to its calling > > scope without fear of name clashes, it has to use unique local names > > (e.g., a namespace). But with dynamic scoping it remains the case > > that you can't add a name to the global scope if any function in the > > call chain has pre-empted that name -- named references don't change > > this basic rule. > > I really don't follow, dynamic scoping should make things easier > here. Consider the case where your function is called 2 or more levels removed from the global scope. > but having namerefs that can't create globals seems like a very > severe limitations to me It's the same limitation as () { local foo unset foo () { foo=3Dbar } } which despite the appearance of the assignment in the nested function, doesn't create a global foo, it resurrects the caller's local foo. Ksh has static local scope (and an entirely different internal representation of parameters) so the local foo makes no difference to the nested function.