Tcl can do some pretty unusual and remarkable things, so it's worth asking:
Is there some way to make all variables global?
Automatically?
Even inside procs?
Maybe some introspection or everything-is-a-string Tcl sort of trick?
Tcl can do some pretty unusual and remarkable things, so it's worth asking: >>
Is there some way to make all variables global?
What is the purpose?
If you really want every variable to be global, then prefix all your >variables with ::
On Sat, 17 Dec 2022 16:11:46 -0000 (UTC), Kenny McCormack wrote:
Tcl can do some pretty unusual and remarkable things, so it's worth
asking:
Is there some way to make all variables global?
What is the purpose?
Usually, when you ask "Is it possible to do <X>?" and the first response
is "Why do you want to do <X>?", then you can assume that the answer to
your question is "No".
...
If you really want every variable to be global, then prefix all your
variables with ::
That's actually quite useful. That you can make a variable global by
prefixing the name with ::.
I've always done it the old-fashioned (hard) way - of having to declare it >> global in every proc that uses it. Using :: looks like it would be a lot
easier. Thanks.
I am aware of the :: method. I was rather wondering if there might be something like, whenever a variable is created, apply this or that to it.
Maybe it doesn't exist, but sounds a lot like a feature that Tcl would have.
Tcl can do some pretty unusual and remarkable things, so it's worth
asking:
Is there some way to make all variables global?
What is the purpose?
Usually, when you ask "Is it possible to do <X>?" and the first response
is "Why do you want to do <X>?", then you can assume that the answer to
your question is "No".
...
If you really want every variable to be global, then prefix all your >variables with ::
That's actually quite useful. That you can make a variable global by prefixing the name with ::.
I've always done it the old-fashioned (hard) way - of having to declare it global in every proc that uses it. Using :: looks like it would be a lot easier. Thanks.
Regarding 'everything-is-a-string' I don't want to start a discussion
here, but in my point of view this is a misleading statement,
and not 'True' for Tcl.
The Beauty of Tcl is, that you don't need a format call to display
the string, due to the fact that everything is hold in a Tcl_Obj (The
display value, and the 'double' in C, and this is dynamic).
Even in other languages, like C, Phyton, your source code is 'TEXT',
a number like '10.25' is text in your source code. A SQL select is
most of the time 'Text' as well, HTML is text and so on
...
Mole Cool <molecool1058@googlemail.com> wrote:
Regarding 'everything-is-a-string' I don't want to start a discussionMore correctly, it is an "age-old" statement that has come to be Tcl's "catchphrase" even still.
here, but in my point of view this is a misleading statement,
Except it is not because of the Tcl_Obj, it is because the Tcl_Obj
system was designed to emulate the pre-8.0 Tcl 'everything is a string' semantics.
Tcl can do some pretty unusual and remarkable things, so it's worth asking:
Is there some way to make all variables global? Automatically? Even inside procs? Maybe some introspection or everything-is-a-string Tcl sort of trick?
} else {
append new_sig "\{$new_arg \"$xinit\"\} "
}
} else {
set xinit [lindex $x 1]
append new_sig "\{$new_arg \"$xinit\"\} "
}
The idea of creating a "gset" (set global) proc is something obvious
Tcl can do some pretty unusual and remarkable things, so it's worth asking:
Is there some way to make all variables global? Automatically? Even inside procs? Maybe some introspection or everything-is-a-string Tcl sort of trick?
* saitology9 <saitology9@gmail.com>
| # the trick is to update or (create) proc arg's in the global scope
| # and then evaluating the proc body with uplevel.
I would say that the proc's args should *not* go into the global
namespace, since they do exist as locals inside the proc and cause no problem.
At least *I* would certainly not want to override some arbitrary global variable which just happens to have the same name as the proc arg of
some random function.
However, this certainly will help to achieve the horror Rich predicted elsethread :-)
Yes it is pure hell. There are a few "programming languages" like this
(e.g. SPECS instrument control system) which have no local variables and
it becomes hell whenever you decide to import a library written by
someone else.
What also cannot be prevented is that someone begins a script/program with some kind of inventory of all variables used in the program and making them all globals with the 'global' command. Any possible implementation of what
I asked would merely automate something that already can be done although
in a tedious manner.
I would say that the proc's args should *not* go into the global
namespace, since they do exist as locals inside the proc and cause no
problem.
I think you can circumvent this by putting the vars into a special
namespace. Since all vars should be global, namespaces will not occur.
I think there is still a "bug", though - the "uplevel 1" will not work
when it is a nested procedure call. "uplevel #0" would do the trick to
run the code at global scope. I haven't tested it, so might be wrong ;)
On Mon, 19 Dec 2022 15:52:43 +0100, Christian Gollwitzer wrote:
Yes it is pure hell. There are a few "programming languages" like this
(e.g. SPECS instrument control system) which have no local variables and
it becomes hell whenever you decide to import a library written by
someone else.
I'm pretty sure there is a very large number of programs/scripts out there that will never ever interact with any external libraries or packages or remote machines and will never be shared. That kind of use exists, it's very common and cannot (and should not) be prevented.
What also cannot be prevented is that someone begins a script/program with some kind of inventory of all variables used in the program and making them all globals with the 'global' command. Any possible implementation of what
I asked would merely automate something that already can be done although
in a tedious manner.
I would say that the proc's args should *not* go into the global
namespace, since they do exist as locals inside the proc and cause no problem.
However, this certainly will help to achieve the horror Rich predicted elsethread :-)
On Mon, 19 Dec 2022 15:52:43 +0100, Christian Gollwitzer wrote:
Yes it is pure hell. There are a few "programming languages" like this
(e.g. SPECS instrument control system) which have no local variables and
it becomes hell whenever you decide to import a library written by
someone else.
I'm pretty sure there is a very large number of programs/scripts out there that will never ever interact with any external libraries or packages or remote machines and will never be shared. That kind of use exists, it's very common and cannot (and should not) be prevented.
What also cannot be prevented is that someone begins a script/program with some kind of inventory of all variables used in the program and making them all globals with the 'global' command. Any possible implementation of what
I asked would merely automate something that already can be done although
in a tedious manner.
On Mon, 19 Dec 2022 15:52:43 +0100, Christian Gollwitzer wrote:
Yes it is pure hell. There are a few "programming languages" like this
(e.g. SPECS instrument control system) which have no local variables and
it becomes hell whenever you decide to import a library written by
someone else.
I'm pretty sure there is a very large number of programs/scripts out there that will never ever interact with any external libraries or packages or remote machines and will never be shared. That kind of use exists, it's very common and cannot (and should not) be prevented.
What also cannot be prevented is that someone begins a script/program with some kind of inventory of all variables used in the program and making them all globals with the 'global' command. Any possible implementation of what
I asked would merely automate something that already can be done although
in a tedious manner.
How about defining a dictionary variable in the global scope whose keys************************
are the global variable names? Without thinking too much it seems easy
to use an editor command to then do the code modifications:
s/globalVar/g["globalVar"]/g
where "g" is the dictionary variable in global scope. It certainly
reduces the likelihood of name collisions in the global namespace at the expense of reformatting the code.
The 'global' command would have to be done inside of each procedure.
Doing 'global' at the global scope level has no effect.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 463 |
Nodes: | 16 (2 / 14) |
Uptime: | 157:16:28 |
Calls: | 9,384 |
Calls today: | 4 |
Files: | 13,561 |
Messages: | 6,096,000 |