• an additional if command

    From Mark Summerfield@m.n.summerfield@gmail.com to comp.lang.tcl on Wed Oct 22 08:50:54 2025
    From Newsgroup: comp.lang.tcl

    I often find myself writing things like this:

    set x [mycommand ...]
    if {$x ne ""} { ... }

    I'd like to be able to do this instead:

    iff {set x [mycommand ...]} { ... }

    To this end I've created a couple of commands shown below.

    Are these reasonable? I've never used uplevel before so
    I don't know if there are any problems that I've missed.

    #!/usr/bin/env tclsh9

    proc main {} {
    iff {set a [expr {5 + -5}]} { puts FAIL }
    iff! {set b [expr {5 + -5}]} { puts "$b OK" }
    iff {set c {}} { puts FAIL }
    iff {set d [list a b c]} { puts "$d OK" }
    iff {set e false} { puts FAIL }
    iff {set f true} { puts "$f OK" }
    iff {set g ""} { puts FAIL }
    iff {set h "H"} { puts "$h OK" }
    }

    proc iff {setcmd tcode {ncode ""}} {
    set x [uplevel 1 $setcmd]
    switch $x {
    0 - false - no - off - "" - {} {
    if {$ncode ne ""} { uplevel 1 $ncode }
    }
    default { uplevel 1 $tcode }
    }
    }

    proc iff! {setcmd tcode {ncode ""}} {
    set x [uplevel 1 $setcmd]
    switch $x {
    0 - false - no - off - "" - {} { uplevel 1 $tcode }
    default { if {$ncode ne ""} { uplevel 1 $ncode } }
    }
    }

    main
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Schelte@nospam@wanadoo.nl to comp.lang.tcl on Wed Oct 22 11:29:14 2025
    From Newsgroup: comp.lang.tcl

    On 22/10/2025 10:50, Mark Summerfield wrote:
    I often find myself writing things like this:

    set x [mycommand ...]
    if {$x ne ""} { ... }

    I'd like to be able to do this instead:

    iff {set x [mycommand ...]} { ... }

    You are aware that `set` returns the new value of the variable? So you
    can just do:

    if {[set x [mycommand ...]] ne ""} { ... }


    Schelte.

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Ralf Fassel@ralfixx@gmx.de to comp.lang.tcl on Wed Oct 22 11:30:10 2025
    From Newsgroup: comp.lang.tcl

    * Mark Summerfield <m.n.summerfield@gmail.com>
    | To this end I've created a couple of commands shown below.

    | Are these reasonable? I've never used uplevel before so
    | I don't know if there are any problems that I've missed.
    --<snip-snip>--
    | proc iff {setcmd tcode {ncode ""}} {
    | set x [uplevel 1 $setcmd]
    | switch $x {

    Better use
    switch -- $x
    here, in case the [set x] returns something with a leading "-".

    Other than that, looks fine to me.

    An exercise would be to check whether using
    llength $ncode
    brings any advantage with respect to shimmering over using
    $ncode ne ""

    R'
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Mark Summerfield@m.n.summerfield@gmail.com to comp.lang.tcl on Wed Oct 22 16:03:46 2025
    From Newsgroup: comp.lang.tcl

    On Wed, 22 Oct 2025 11:29:14 +0200, Schelte wrote:

    On 22/10/2025 10:50, Mark Summerfield wrote:
    I often find myself writing things like this:

    set x [mycommand ...]
    if {$x ne ""} { ... }

    I'd like to be able to do this instead:

    iff {set x [mycommand ...]} { ... }

    You are aware that `set` returns the new value of the variable? So you
    can just do:

    if {[set x [mycommand ...]] ne ""} { ... }


    Schelte.

    Thanks for reminding me of that; I'd forgotten.
    (Thanks also to Ralf for his reply.)

    I've realised that my iff command was really going against Tcl's grain;
    in effect trying to make empty lists and empty strings false as they
    are in Python (which I'm more used to).

    I'll try to use your approach if {[set ...] ne ""} { ... }; or in
    fact I'll try to return 0 for empty lists or empty strings so I can
    simplify to if {![set ...]} { ... }

    Thanks again!
    --- Synchronet 3.21a-Linux NewsLink 1.2