« Return to Thread: expr functions

Re: expr functions

by Griffin, Brian :: Rate this Message:

| View in Thread

On Aug 16, 2012, at 12:35 AM, Frédéric Bonnet wrote:

> Le 14/08/2012 16:30, Brian Griffin a écrit :
>> This question just occurred to me:  why is (should) expr functions (be)
>> restricted to ::tcl::mathfunc:: ?  Why could it not use the same scoping
>> rules Tcl uses for commands?
>>
>> I got to this point after noticing, first, a lot of occurrences of "if
>> {[expr {$a + $b}] > 3} ...", i.e. unnecessary calls to [expr] within
>> [expr].
>> After cleaning these up, I noticed unclean-able cases of "if {[lindex
>> $list [expr {$a+$b}]] ne "foo"} ..." where the recursive call to [expr]
>> is unavoidable.  This is solved by creating an alias to lindex in
>> ::tcl::mathfunc::, so now it can be written:  "if {lindex($list,$a+$b)
>> ne "foo"} ..."
>> Then I started noticing "if {[myfunc [expr {$a+$b}]] == 3} ...".  At
>> this point it starts getting ridiculous trying to simplify and make
>> these expressions more reasonable to read, except if [expr] would just
>> use the normal search rules to find functions...
>>
>> Thoughts?
>
> A quick & dirty proof of concept with unknown handlers:
>
>
>     proc mathproc {cmd args} {
>         if {[namespace qualifiers $cmd] eq "tcl::mathfunc"} {
>             return [uplevel [list [namespace tail $cmd]] $args]
>         }
>         uplevel [list ::unknown $cmd] $args
>     }
>     namespace unknown mathproc
>
>     set l [expr {list(1,2,3)}]
>     => 1 2 3
>
>     expr {lindex($l,2)}
>     => 3
>
>     expr {expr(join($l,"+"))}
>     => 6
>
>
> I love Tcl !!!

Thanks Frédéric, this is awesome!  I'm sad that I didn't think of this.

I love Tcl too!

-Brian


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Tcl-Core mailing list
Tcl-Core@...
https://lists.sourceforge.net/lists/listinfo/tcl-core

 « Return to Thread: expr functions