?if ( ..a default cond: ( default -- new/f ) true: ( ..a new -- ..b ) false: ( ..a default -- ..b ) -- ..b )
Factor handbook » The language » Combinators » Conditional combinators

Prev:unless* ( ..a ? false: ( ..a -- ..a x ) -- ..a x )
Next:?when ( ..a default cond: ( ..a default -- ..a new/f ) true: ( ..a new -- ..a x ) -- ..a default/x )


Vocabulary
kernel

Inputs
defaultan object
condan object
truean object
falsean object


Outputs
None

Warning
The old ?if word can be refactored:
[ .. ] [ .. ] ?if or? [ .. ] [ .. ] if


Word description
Calls cond on the default object and if cond outputs a new object then the true quotation is called with that new object. Otherwise, calls false with the old object.

Examples
Look up an existing word or make an error pair:
USING: arrays definitions kernel math prettyprint sequences vocabs.parser ; "+" [ search ] [ where first ] [ "not found" 2array ] ?if .
"resource:core/math/math.factor"

Try to look up a word that doesn't exist:
USING: arrays definitions kernel math prettyprint sequences vocabs.parser ; "+++++" [ search ] [ where first ] [ "not found" 2array ] ?if .
{ "+++++" "not found" }


See also
?when, ?unless

Definition

: ?if
( ..a default cond: ( default -- new/f ) true: ( ..a new -- ..b ) false: ( ..a default -- ..b ) -- ..b )
[ ?transmute ] 2dip if ; inline