Version 5 of unless

Updated 2004-05-04 18:21:15

proc unless {condition body} {

        uplevel [list if "!($condition)" $body]
    }

example:

    unless {$tcl_version >= 8.4} {
        error "package requires Tcl version 8.4 or greater"
    }

Lars H: An alternative implementation, which avoids shimmering:

    proc unless {condition body} {
        uplevel 1 [list if $condition then {} else $body]
    }

glennj: additionally, provide an else clause...

    proc unless {cond body args} {
        set else_body {}
        if {([llength $args] == 2) && ([lindex $args 0] eq "else")} {
            set else_body [lindex $args 1]
        } elseif {[llength $args] > 0} {
            error "usage: [lindex [info level 0] 0] expr body1 ?else body2?"
        }
        uplevel 1 [list if $cond then $else_body else $body]
    }

Category Command