Another Ruby-inspired Tcl construct

Inspired by the Ruby construct where you can embed something like

#{"<em>some string</em>" if <em>condition</em>}

inside a string, I have started using this syntax in Tcl from time to time:

DELETE FROM gcms_rels
WHERE revision_id = :revision_id
[if { [exists_and_not_null rel_options] } {
    set x "AND related_item_id IN ([join $rel_options ", "])"

It is pretty much exactly the same thing as in Ruby, and also very similar to the use of ad_decode that we’ve been using in OpenACS for a while. But it is superior to ad_decode because it is more readable, and because you can have elseif, switch, or any other construct in there.

The only ugly thing is the dummy set x part, which is required because, unlike Ruby, Tcl will interpret "foo" on a line by itself as the name of a command to be executed. I also tried using return, but that blows out of the entire proc, so isn’t usable.

If you have a cleaner solution, I would like to hear about it.


Lars Pind

But then you would have to wrap your string in curly braces instead, or substitution would be performed twice ...
Read more
Read less
Michael Cleverly

First thought--define a helper proc called 'then': <blockquote> <tt>proc then {x} {return $x}</tt> </blockquote> then you can write: <blockquote> <pre><tt>DELETE FROM gcms_rels WHERE revision_id = :revision_id [if { [exists_and_not_null rel_options] } { then "AND related_item_id IN ([join $rel_options ", "])" }]</tt></pre> </blockquote>
Read more
Read less

Leave a comment