Like I said some time ago, I am enjoying using namespaces because I
create them then eval stuff inside them and everything works fine.
There is only one thing that is bugging me:
proc SomeProc {arg1 arg2} {
namespace eval MYNS {
if {$arg1 > $arg2} {puts "1 is greater than 2!"}
}
}
Of course it doesn't work.
This works:
proc SomeProc {arg1 arg2} {
set MYNS::arg1 $arg1
set MYNS::arg2 $arg2
namespace eval MYNS {
if {$arg1 > $arg2} {puts "1 is greater than 2!"}
}
}
Since I use a lot of procs and namespaces in my code, I find myself
doing it a lot. It works, but it's only clunky, I often feel somehow like I am doing something that I am not really supposed to. It's kludgy.
Is there a smarter way of handling that?
Notes:
1. In the second code snippet, the MYNS namespace has been created
earlier in the code.
2. I can't create MYNS::SomeProc because in many cases the namespace
is created and destroyed on the fly so its name is unpredictable.
Depends on just what 'that' is which you are referring. One thing that
your last few posts has indicated is that you seem to be running code >directly in the namespace eval script.
While that does work, it really is not the intent of namespaces.
The basic intent is for them to look like this:
namespace eval NS {
variable var1 [init var1]
variable var2
proc abc {a1} {
variable var1
#do something with a1 and var1
}
proc def {b1} {
variable var2
#do something with b1 and var2
}
}
2. I can't create MYNS::SomeProc because in many cases the namespace
is created and destroyed on the fly so its name is unpredictable.
Sure you can. In order to create MYNS on the fly, you have to create >'something' as its name, so you can create 'SomeProc' inside of it, and
call it using the created name just fine.
$ rlwrap tclsh
% set nsname [expr {rand()}]
0.6244215865733203
% proc abc {nsname} {
namespace eval $nsname {
proc SomeProc {arg1} {
puts "Hi from SomeProc, my arg1='$arg1'"
}
}
}
% abc $nsname
% ${nsname}::SomeProc Hello
Hi from SomeProc, my arg1='Hello'
On Sat, 27 Jan 2024 05:24:47 -0000 (UTC), Rich wrote:
Depends on just what 'that' is which you are referring. One thing that >>your last few posts has indicated is that you seem to be running code >>directly in the namespace eval script.
By 'that' I mean having the code inside a namespace use data that has
been created outside of it. The way I'm doing it is exactly like using
::tsv with threads. But threads have completely separate interpreters. Namespaces don't.
While that does work, it really is not the intent of namespaces.
So maybe you're just telling me that what I'm doing is unusual at
heart and the way I'm doing it is the only unusual way of carrying on
my unusual approach.
The basic intent is for them to look like this:
namespace eval NS {
variable var1 [init var1]
variable var2
proc abc {a1} {
variable var1
#do something with a1 and var1
}
proc def {b1} {
variable var2
#do something with b1 and var2
}
}
You put procs inside namespaces. That's what every example out there
does. I have the opposite, I'm dealing with namespaces inside procs. Different laws of physics.
2. I can't create MYNS::SomeProc because in many cases the
namespace is created and destroyed on the fly so its name is
unpredictable.
Sure you can. In order to create MYNS on the fly, you have to create >>'something' as its name, so you can create 'SomeProc' inside of it,
and call it using the created name just fine.
$ rlwrap tclsh
% set nsname [expr {rand()}]
0.6244215865733203
% proc abc {nsname} {
namespace eval $nsname {
proc SomeProc {arg1} {
puts "Hi from SomeProc, my arg1='$arg1'"
}
}
}
% abc $nsname
% ${nsname}::SomeProc Hello
Hi from SomeProc, my arg1='Hello'
That works in theory but not in practice. Every "tab" has its own
namespace whose name is generated on the fly (clock milliseconds).
Every new tab has to go through a certain... procedure. A Tcl proc, naturally.
I have to prescribe that procedure. I can't create and
name that proc inside a namespace that doesn't exist yet.
I think it's very obvious that I should prescribe one single global
proc and execute it inside the countless namespaces that will be born
and die at runtime.
Unless I rename that proc.
proc SomeProc {} {
do whatever
}
set id [clock milliseconds]
namespace eval $id {}
rename SomeProc id::SomeProc
namespace eval $id {
SomeProc
}
But that is not what your example suggests and I am not very confident
that renaming that crucial proc every time a tab is created will be
a good idea.
If only I could clone that proc... Damn, you were right. I am doing
OOP!
OK, it's an idea. I can clone procs. I ran a search and found something interesting about that. I will investigate it.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 497 |
Nodes: | 16 (2 / 14) |
Uptime: | 68:29:32 |
Calls: | 9,766 |
Calls today: | 7 |
Files: | 13,747 |
Messages: | 6,186,073 |