From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3404 invoked by alias); 15 Mar 2018 21:46:36 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: List-Unsubscribe: X-Seq: 23247 Received: (qmail 8244 invoked by uid 1010); 15 Mar 2018 21:46:36 -0000 X-Qmail-Scanner-Diagnostics: from aurora-borealis.phear.org by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(94.242.205.164):SA:0(-1.9/5.0):. Processed in 10.363272 secs); 15 Mar 2018 21:46:36 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_PASS, T_DKIM_INVALID,T_RP_MATCHES_RCVD autolearn=ham autolearn_force=no version=3.4.1 X-Envelope-From: khatar@phear.org X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | X-Virus-Scanned: amavisd-new at phear.org DKIM-Filter: OpenDKIM Filter v2.10.3 aurora-borealis.phear.org D6B1110F21 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=phear.org; s=20180217; t=1521150380; bh=osBObQhW93RboWCXmbhDWRDcBTXulzj0CSuYt5UVM7s=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=sQtloqKUkdzFZHiqn4dGboz4pHXPfuHVfCcG19kVEI+bGwWXvehN3/OS3U16AXOkr ffqg8qxFdhXktC48ddmspN7es9JeuRruETUYm7Lk44HHVOd0KX6tQ447Lbfr/sDHZH 55dwymvMfpsa8BUKZ43a9thp/c5uPFZCoBqsJo0kv+U96hPk1IEWNtfhzz/TSdwpVm sfyNYvvA/YbRhzQt39b4MPGO+FALdbMBHqv7pH/m9NGNpqieijWTMH3PcmJHcnhQot dQtOXk3nSncvD8sctvL6263/jZ0Q27LUs7BIk+6NOWvyxmuK9fsChEiebDrkhIc+hV +JJ80XOeVTr3g== Date: Thu, 15 Mar 2018 22:46:18 +0100 From: Marc Chantreux To: Ray Andrews Cc: zsh-users@zsh.org Subject: Re: real time alias? Message-ID: <20180315214618.5exweubdq3d5akzo@prometheus.u-strasbg.fr> References: <98aa0638-97ed-dfe0-8dd2-39129d02c084@eastlink.ca> <9001d78e-91fd-8505-518f-27247462d3c2@eastlink.ca> <20180315214450.sle4bsb3ab6dt56i@prometheus.u-strasbg.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180315214450.sle4bsb3ab6dt56i@prometheus.u-strasbg.fr> User-Agent: NeoMutt/20170113 (1.7.2) On Thu, Mar 15, 2018 at 10:44:50PM +0100, Marc Chantreux wrote: > hello, > > > > > If a function calls an alias, if the alias changes, the function must be > > > > resourced, yes? That makes nothing but sense sincethe alias is what it is > > > > at sourcing. An executed script uses the alias in 'real time'. But, is > > > > there a way to make a function also use the real time value of an alias? > > > Sure, I was just wondering if it was possible at all with an alias. > > my advice is: see aliases as macro: don't use it as long as you can > achieve things with functions. one of the reasons is aliases can be used > after a variable expansion and so you can get some surprising behaviors. > > rip () { > print $1 wrote > "$@" > print then $1 died > } > alias stephen='print a brief history of time' > rip stephen hawkings > > then your universe colapse > > stephen wrote > rip:2: command not found: stephen > then stephen died > > but > > rip () { > print $1 wrote > "$@" > print then $1 died > } > stephen () print a brief history of time > rip stephen hawkings > > gives you a good reading advice > > stephen wrote > a brief history of time > then stephen died > > so when are aliases useful? well ... basically when you need something > like a preprocessor. for example if you have a set of functions where > the first arguments are always the same, you can write an alias for it: > > alias user_='local ns=${1?namespace of the user expected} \ > id=${2?the id of the user expected}' > > showid () { user_; print user id is $id } > showns () { user_; print user ns is $ns } > showid > > warns you > > showid: 1: namespace of the user expected > > another example from uze.zsh (https://github.com/zsh-uze) > > warn_ () { local r=$?; print -u2 "$*"; return $r } > die_ () { local r=$?; print -u2 "$*"; exit $r } > alias warn='warn_ at $0 line $LINENO, warning:' > alias ...='{warn unimplemented; return 255}' > alias die='die_ died at $0 line $LINENO:' > > now i can write > > allowed () false > > do_stuff () { > if {allowed} { > print i do stuff > } else { > # what to do then ? > ... > } > } > > do_stuff > > so i have this message: > > at do_stuff line 5, warning: unimplemented > > hth, > marc > > > you ask, there might be some option or something whereby a function is > > instructed to grab an alias at runtime sorta the way the value of a variable > > is grabbed at runtime. > > you can use both functions and arrays > > show () { > print "hello, i'm a particle and i'm" > position > } > position () print here and there > show > position () print elsewhere > show > > gives > > hello, i'm a particle and i'm > here and there > hello, i'm a particle and i'm > elsewhere > > also > > show () print -l "hello, i'm a particle and i'm" $position > position=( here and there ) > show > position=( elsewhere ) > show > > gives > > hello, i'm a particle and i'm > here > and > there > hello, i'm a particle and i'm > elsewhere > > don't forget that in crontrary of other shells, zsh takes arrays and > word spliting seriously so you can store a whole command into an array. > this is lovely when you build one step by step > > academic () true > faster () true > dryrun () true > > dryrun && compiles=( print cc ) > || compiles=( cc ) > > academic && compiles+=( -Wall -std=c99 ) > faster && compiles+=( -O2 ) > compiles+=( $src $dest ) > $compiles > > hth > marc