ClojureDocs

Nav

Namespaces

when

clojure.core

Available since 1.0 (source)
  • (when test & body)
Evaluates test. If logical true, evaluates body in an implicit do.
4 Examples
user=> (when (= 1 1) true)
true

user=> (when (not= 1 1) true)
nil
user=> (def has-value (when true
                            (println "Hello World")
                            "Returned Value"))
Hello World
#'user/has-value

user=> has-value
"Returned Value"

;; See examples for "if" explaining Clojure's idea of logical true
;; and logical false.
;; When is a macro of (if .. do ..)

user=> (macroexpand '(when 1 2 3 4))
(if 1 (do 2 3 4))

;; if 1 is true, do will evaluate 2 3 4, but return values of 2 and 3 would be 
;; ignored. Value of 4 (last value) would always be returned. 
;; See https://clojuredocs.org/clojure.core/do for details

user=> (if 1 (do 2 3 4))
4

See Also

Evaluates test. If logical false, evaluates body in an implicit do.

Added by mmwaikar

bindings => binding-form test When test is true, evaluates body with binding-form bound to the va...

Added by mmwaikar

Evaluates test.

Added by jafingerhut

Coerce to boolean

Added by MicahElliott

bindings => binding-form test When test is not nil, evaluates body with binding-form bound to th...

Added by MicahElliott
0 Notes
No notes for when