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

class code {#class-code}

Executable code in PCE is represented by PCE objects. This approach allows us to define new executable code for the PCE system from the host language (Prolog) without extending the interface between PCE and Prolog.

The super-class code is a super-class of everything that is executable in PCE. Code objects are normally attached to method objects, or to objects to define the behaviour for handling some events. For example, a button object has a code object attached to it that determines the behaviour of the button when it is depressed. Finally, code objects are used as conditions to the iteration and finding methods chain->for_all, chain <-find_all, etc. Examples:

Attaching a code object as an action to an interface object:

?- new(D, dialog('Hello World')),
   send(D, append,
        button(quit, message(D, destroy))),
   send(D, open).

Using a code object to sort a chain of objects by name:

send(Chain, sort,
     ?(@arg1?name, compare, @arg2?name)).

Using a code object to define a method:

send(class(person), send_method,
     send_method(print, vector('on=object',
                 message(@arg1, format,
                         'Name:\t%s\nAge:\t%s\n',
                         @receiver?name,
                         @receiver?age))))

This example defines a method on a hypothetical class person called print that formats the name and age of the person to a specified object. Note that XPCE/Prolog as well as XPCE/Lisp define an interface which allows for defining methods in a programmer-friendly fashion. See pce_begin_class/3.

Code objects are normally activated from the object they are attached to. There are two ways to activate a code object. One that pushes an variable frame (->forward) and one that uses the current variable frame ->execute.

Code objects normally return either success or failure, with the exception of the subclasses of class function, which -like get()- returns a true value.

@see class function @see topic Parameters @see class method

Send methods {#class-code-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.