Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2006-11-04
02:30 • Ticket [1587442fff] let upvar only create variables in the current context s... artifact: b1ec952881 user: dgp
2006-11-03
22:22 • Ticket [1587442fff]: 4 changes artifact: 691fe4ed30 user: msofer
22:18 • Ticket [1587442fff]: 4 changes artifact: 1abb942776 user: msofer
19:10 • Ticket [1587442fff]: 4 changes artifact: 978a1c9ebf user: kaitzschu
2006-10-31
03:05 • Ticket [1587442fff]: 4 changes artifact: 32a446aa76 user: msofer
03:05 • Add attachment upvar.patch to ticket [1587442fff] artifact: b9f4f90668 user: msofer
2006-10-30
20:05 • New ticket [1587442fff] let upvar only create variables in the current context</... artifact: 497900992a user: msofer

let upvar only create variables in the current context

Open1587442fffffffffffffffffffffffffffffffffCreated 20 years agoUpdated 20 years ago
Descriptionplain2006-10-30 20:05:56
Currently 
   upvar 1 x ::a::y
will create a variable y in namespace a, linked to the
variable x in the caller's context.

This "action at a distance" is undocumented and
possibly confusing.  It is better IMO to force upvar to
only create variables in the current context (namespace
or proc call frame).

Attached a patch that implements and documents this
feature. Note that the tests var-3.7/8 test for the
current behaviour.
msoferplain2006-10-31 03:05:56
File Added - 200677: upvar.patch
kaitzschuplain2006-11-03 19:10:37
Logged In: YES 
user_id=966098

So how does one do (centralized namespace variable templating without copying)

set tcnt -1
foreach ns {ns1 ns2 ns3} {
 for {set cnt 0} {$cnt < 5} {incr cnt} {
  foreach arn {arr lis dic} {
   upvar #0 [namespace current]::$arn[incr tcnt] ::${ns}::$arn$cnt
  }
 }
}

after this implementation when ns and cnt are variadic? Instead of upvaring these to heavens high one is supposed to

namespace inscope ::$ns upvar [namespace current]::$arn$[incr tcnt] $arn$cnt

or something even remotely resembling? Is this "remotely controlled action at distance" then documented (can I trust these links 
do exist) or am I simply "doing the wrong thing"?
msoferplain2006-11-03 22:18:19
Logged In: YES 
user_id=148712

The idea is that you [namespace eval] in the namespace where
you want your variables to be created; I would code your
algorithm as

set tcnt -1
foreach ns {ns1 ns2 ns3} {
    set lst [list namespace upvar [namespace current]]
    for {set cnt 0} {$cnt < 5} {incr cnt} {
        foreach arn {arr lis dic} {
            lappend lst $arn[incr tcnt] $arn$cnt
        }
    }
    namespace eval $ns $lst
}

On the other hand, your current algorithm I would also code
somewhat differently. Note that
  upvar #0 [namespace current]::x ::a::y
is equivalent to the faster (and IMO clearer)
  upvar 0 x ::a::y

So, I would now code yours as

set tcnt -1
set lst [list upvar 0]
foreach ns {ns1 ns2 ns3} {
    for {set cnt 0} {$cnt < 5} {incr cnt} {
        foreach arn {arr lis dic} {
            lappend lst $arn[incr tcnt] ::${ns}::$arn$cnt
        }
    }
}
eval $lst
msoferplain2006-11-03 22:22:06
Logged In: YES 
user_id=148712

Forgot to mention: [namespace upvar] is in 8.5; it has
nothing to do with this feature request really.
dgpplain2006-11-04 02:30:50
Logged In: YES 
user_id=80530

I haven't had the cycles to spare
to check this out rigorously, but
it raises my suspicion that it will
thoroughly break things done in the
Tk script library.  Please check that
over carefully before proceeding.
Log in (anonymous is fine) to add a comment.

Attachments: