Next: Self and super, Up: Methods [Index]
This section describes the C functions and macros that methods use to interface with the Ctalk run time libraries and with each other.
At this point, the method API is still developing, and relies heavily on C library functions.
This example shows method +
of class Integer
, which
adds two Integer objects, creates an object for the result, and
returns the result.
Integer instanceMethod + add (int i) { OBJECT *op1, *op2; op1 = self value; op2 = i value; methodReturnInteger(atoi(op1->__o_value) + atoi(op2->__o_value)) }
The keyword self
refers to the receiver of the method.
See Self and super.
The variables op1
and op2
are the value
instance
variables of the receiver and the argument. Ctalk has a method,
value
(class Object
), which returns the value
instance variable, but you can also refer to instance variables by
sending an instance variable’s name as a message to an object.
Internally, the receiver, arguments, and return value of a method are
all ‘OBJECT *’. When you use C functions with objects, you use
the members of an OBJECT type, a struct.
(See OBJECT typedef.) If you declare an object as a C OBJECT *,
then the
method uses it like any other C struct *.
All methods return an ‘OBJECT *’ as the result. The macro
methodReturnInteger
defines the object that the method
returns. See Return values.
The add
method is in classes/Integer
. The file
include/object.h
contains the declaration of the ‘OBJECT’
type.