Re: Syntax, expressiveness and the beauty of Tcl
- From: "slebetman@xxxxxxxxx" <slebetman@xxxxxxxxx>
- Date: 30 Oct 2006 23:26:55 -0800
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
.
- Follow-Ups:
- Re: Syntax, expressiveness and the beauty of Tcl
- From: Stephan Kuhagen
- Re: Syntax, expressiveness and the beauty of Tcl
- References:
- Syntax, expressiveness and the beauty of Tcl
- From: Stephan Kuhagen
- Re: Syntax, expressiveness and the beauty of Tcl
- From: slebetman@xxxxxxxxx
- Re: Syntax, expressiveness and the beauty of Tcl
- From: Stephan Kuhagen
- Syntax, expressiveness and the beauty of Tcl
- Prev by Date: Re: Syntax, expressiveness and the beauty of Tcl
- Next by Date: Re: Syntax, expressiveness and the beauty of Tcl
- Previous by thread: Re: Syntax, expressiveness and the beauty of Tcl
- Next by thread: Re: Syntax, expressiveness and the beauty of Tcl
- Index(es):
Relevant Pages
|