src/swipl-devel/packages/xpce/man/refmanual/md/when.md

class when {#class-when}

When executed, a when object tests the condition and then evaluates either the then- or else-function, returning its result. This construct is similar to C and similar languages Condition ? Then : Else.

The example below illustrates how a method number <-absolute could be attached to class number to return its absolute value:

?- send(class(number), get_method,
        get_method(absolute, int, new(vector),
           when(@receiver < 0,
            -(@receiver),
            @receiver?value))).

?- new(N, number(-4)),
   get(N, absolute, V).     ==> V = 4.

@see class if

Instance variables {#class-when-instvars}

Send methods {#class-when-send}



Try the rswipl package in your browser

Any scripts or data that you put into this service are public.

rswipl documentation built on June 16, 2026, 5:07 p.m.