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 F3326231AA for ; Mon, 4 Mar 2024 21:00:07 +0100 (CET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1709582407; b=qRRwhA1c0Dl1vy7w/yKWPtDQaokB7f4ykE5oPSKGk8cIujG8lStfeL/dffJA2dM8iyrSjWkNhx imphd6j9kJNH1Wif2dbml144cxAneibGbyho2/qkwk/yMVJmxPRQRS5KkTy7tqAFnZg7BSoFOp ZPwrBVtdUC1fLWDrka1wyh4dMNjOLdwNgjXnl/lAwz3Srq55rg+LBvJrGoe0/FF36dpAYJq5lO AbSuOHfVEUXTVKpoYEro2BZLRbwTHT1cv7Lzdlrv8qvg61rWiNLJ0vJK3O0mzwnYT+SoBjJ7Q8 jmVN9Nj0caNzAmh96/88pYLYriex1nYOn1jXB44MFaPntQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (relay5-d.mail.gandi.net) smtp.remote-ip=217.70.183.197; 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=1709582407; bh=DgAQRSqrbwEYuOwwn+e91VgiKqkmvo8VOiYaC4HBkBM=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:DKIM-Signature; b=gOnpIIlVaHbBnXM3VTn2hQhh2yNJbP8rWSv9ycX2CEjJr9LRsz4i8N9af7wA4vZKarEYGeOd3b 6e8m054gDuRhQBtT01bN58A0t7eKiPkxyeqH5YO05Eawc0j9GdGo97nX6NcCwDOHP5vJcobOys LuqM+j26f98tj2293EWMUG3wev4rrwiwqmjxMqz4uziw7IgasuguS525AapDtM1hp+DLeowqC5 s8pbMCxWpB7MOiqVpACxYieCUgn6mvB2RsUtcUEZVmAMU6zK9kdxwy6JMGHZeTD/ZaGjNO8HHB 4QTL0ziPOJ2ukUjjVhyouh3h89ks4Y8+0u+5zKPnNqPnvA==; 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-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=DH0WWlyy3LEIehb36uvctbrv4SsqgItH7b/tmcdYBn4=; b=L7PXpOZFkld2X1IU2KffiiSmJV m3IPcFq5VzIZ5+PXhhZa2UmoGPf7ltm4JqwXFDOnLlgb656oD3WCqv884DgWQ7yn5PCJxgDvs262P GkmSuw1eKJhLlHm4Hyu9LFgi+Fx8C+qET6dWfJ4HDvRjPUyyLb0K48nCvBzVxD+T0DBZvtLiiCfj1 JLM+qIC35WOpmI+XaSqbZoq5YMU2zmSV/kEYlWOQ+k3Qymh0MLxVeFlJFDJUm5IyllyN6drB+0a1F E7C63D/XCAEY0Grk9QrSxEjPtbVAa1Gdc4EWZElbK4NIi8yYqK5GUUCJOJKn4M1yCiQ1xWmadD1ht JpRXUWMw==; Received: by zero.zsh.org with local id 1rhETn-000J1O-AZ; Mon, 04 Mar 2024 20:00:07 +0000 Authentication-Results: zsh.org; iprev=pass (relay5-d.mail.gandi.net) smtp.remote-ip=217.70.183.197; dmarc=none header.from=chazelas.org; arc=none Received: from relay5-d.mail.gandi.net ([217.70.183.197]:60041) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1rhETY-000Ih0-Nj; Mon, 04 Mar 2024 19:59:53 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id 1B95F1C0002; Mon, 4 Mar 2024 19:59:51 +0000 (UTC) Date: Mon, 4 Mar 2024 19:59:49 +0000 From: Stephane Chazelas To: Bart Schaefer Cc: Zsh hackers list Subject: Re: Up-scope named references, vs. ksh Message-ID: <20240304195949.rajtxnltef2stpcx@chazelas.org> Mail-Followup-To: Bart Schaefer , Zsh hackers list References: <20240220210553.g6imt3op6zahz4pa@chazelas.org> <20240221201215.anpjcfav6na55gg6@chazelas.org> <20240301182238.tpyajwblbam5bxw7@chazelas.org> <20240303134413.74c7trikf73g5kjy@chazelas.org> <20240303202756.7axmzy6gkohza2ra@chazelas.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-GND-Sasl: stephane@chazelas.org X-Seq: 52669 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-03-03 14:58:04 -0800, Bart Schaefer: [...] > I've tried several different ways to explain this, so > I'm just going to ignore this going forward. Sorry for being so thick but I still don't get it. As there's still a chance it's just we have different expectations, I'll try a last attempt at clarifying mine below [...] > > 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). If we still have to namespace the names of the nameref variables and any local variables of functions that use namerefs, then we might as well do like in bash/mksh and just follow by 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=$2 > to above the [[ -v $1 ]], then it either bombs, or assigns to the > "wrong" $value. To me, typeset -n ref=var should tie the ref to the var that is currently in scope at the time ref is assigned var. If there's none in scope at the moment, then it should be created unset and undeclared at the global scope at that point, so that at the next ref=value, it's that one that is set and not whatever variable with the same name happened to appear at whatever scope that ref=value was run. In: var=0 f() { local var=1 nameref ref=var echo "$var" } f I expect ref to poing to the local var and get "1" because that's the var that was in scope at the time of ref=var In: var=0 f() { nameref ref=var local var=1 echo "$var" } f I expect 0. In f() { nameref ref=var local var=1 echo "${var-unset}" ref=2 } f echo "$var" I expect unset and 2 [...] > > 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. In: var=0 f1() { local var=1 f2 } f2() { f3 local var=2 } f3() { nameref ref=var echo "$ref" } I expect to see 1 as that's the var that is in scope at the time of ref=var. > > > 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=bar } > } > which despite the appearance of the assignment in the nested function, > doesn't create a global foo, it resurrects the caller's local foo. Which is very much what I expect and agree it would be very wrong for that to create a global 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. (unless the local foo is expected or the nest function uses Bourne-style instead of Korn-style function definition) but yes, I any case I agree with your description of how scoping would work in ksh. -- Stephane