Re: Syntax, expressiveness and the beauty of Tcl



Stephan Kuhagen wrote:
slebetman@xxxxxxxxx wrote:

My personal favourite is to do [interp alias {} expr {} =] so I can
write:

set x [ = $a + $b ]

Can't your not be done with "proc = {..." also? This would work also with +,
-, * and so on. Some kind of operator-"overloading".

Aliases are faster since there is no "call"ing involved. In fact I used
to do it with proc before I found out about aliases.

which in a way looks a little normal again albeit with added [] noise.
Of course for maximum speed I'd have to write:

set x [= { $a + $b }]

But that looks exactly like the original to me.


Well, it doesn't to me. But that's just my personal feelings.

Your pet peeve have just motivated me to see what can be done. There's
probably a page on the wiki for this but I can't be bothered to search
for it right no. Besides, making Tcl do "strange" things can be fun.
Here's my own early attempt to please you:

proc cleanupVar {name1 name2 op} {
rename $name1 {}
}

proc var {name {= =} args} {
upvar 1 $name x
if {[llength $args]} {
set x [expr $args]
} else {
set x {}
}
proc $name args "
upvar 1 $name $name
if {\[llength \$args\]} {
set $name \[expr \[lrange \$args 1 end\]\]
} else {
return \$[set name]
}
"
uplevel 1 [list trace add variable $name unset cleanupVar]
}

OK, the code is very ugly at the moment but it works with the caveat
that you can never have a proc with the same name as a variable
declared with this (since all procs are global). With this you can
write C-like:

proc test {} {
var x
var y = 10

x = $y*2

return $x
}
puts [test]

should output the correct result (20). It even works in recursive procs
(which means that it is safe to use the same variable name in nested
procs):

proc recursiveTest {x} {
var y = $x - 1

if {$y > 0} {
recursiveTest $y
}
puts $y
}
recursiveTest 10

should output the numbers 0 to 9. Another test:

proc test2 {} {
var x = 10
puts "this x belongs to test2 = $x"
}

proc test3 {} {
var x = 100
test2
puts "this x belongs to test3 = $x"
}

test3

output:
this x belongs to test2 = 10
this x belongs to test3 = 100

.



Relevant Pages

  • Re: Thread Syncronization methods
    ... >> It will call Proc in the main Windows thread and wait for the procedure ... Proc takes one untyped variable. ... >> Chris Lenover ...
    (borland.public.delphi.language.objectpascal)
  • Re: upvar doubt
    ... but should be able to access var "a" in it. ... I'm not sure what you want to achive, but upvar can do it. ... imported into a proc with either 'global' or the 'variable' command, ...
    (comp.lang.tcl)
  • Re: Is the word "this" a key word in TCL?
    ... proc foo{ ... what does ",var var" mean? ... They are 'upvar', '#0', the concatenation of the ... idiomatic to make the global variables ...
    (comp.lang.tcl)
  • Re: Is there any way to unset all varibles in a proc at once
    ... >Is there any way to unset all variables which set in a proc. ... Local variables of a proc simply disappear when the proc exits. ... all their names using [info var] and then unsetting them? ...
    (comp.lang.tcl)
  • Re: Test case for proc Which doest return value
    ... "If the procedure doesn't execute an explicit return, ... "var" is a local parameter of your ... test proc, which always exists. ...
    (comp.lang.tcl)