Next: Variable arguments, Previous: Templates, Up: Methods [Index]
Internally, methods return either an OBJECT *
or
NULL
. If you write a method that returns a C
variable, Ctalk normally translates it into an equivalent
object.
Methods that return arrays declared in C generally assign
the C array to an equivalent Array
object. The
Array
object’s size is the same as the array
declaration, regardless of how it the array’s members are
initialized.
The Array
allocation only occurs for
arrays declared with a subscript; that is, a variable
declared as int *
is not always stored in an
Array
object, while a variable declared as
int[size]
is.
Ctalk treats C arrays declared as char[size]
a
little differently. If the method’s definition says the
return class of the method is Array
, then Ctalk
returns an Array
of Character
objects;
otherwise, it returns a String
object.
Because Ctalk does not have equivalent classes for
multi-dimensioned arrays; that is, arrays with more than one
subscript, it does not translate the arrays automatically.
In these cases, the method might return a multi-dimensioned
array by assigning it to a Symbol
object.
Occasionally, you might need to return the result of an
expression that Ctalk can’t translate automatically. In
that case, you can use the eval
keyword to evaluate
the expression when the program is run, as in this example.
MyClass instanceMethod myMethod (void) { ... return eval <expression> }
If a method must create an object of its own to return, the object should have the scope CREATED_PARAM, which tells the Ctalk libraries that the the program only needs the object if it’s the result of a complete expression; if not, the program cleans up the object automatically when it is no longer needed. See __ctalkRegisterUserObject.
return __ctalkCreateObjectInit ("myStringAnswer", "String", "Character", CREATED_PARAM, "The contents of the String object.");
If the program needs to store a return object for longer than the scope that the object is called in, the method can save the object in its object pool with the library function __ctalkRegisterUserObject, which is described below. See __ctalkRegisterUserObject.
String instanceMethod myMethod (void) { OBJECT *return_object; ... Do stuff. ... return_object = __ctalkCreateObjectInit ("myStringAnswer", "String", "Character", CREATED_PARAM, "The contents of the String object."); __ctalkRegisterUserObject (return_object); return return_object; }
Another case may be when a method needs to retrieve an
object reference. In these cases, the method may need to
increase the object’s reference count. However, such a
method can also call __ctalkRegisterExtraObject
to
save the object so its memory isn’t lost later. The
__ctalkRegisterExtraObject
function does not, itself,
set the object’s reference count, and it saves an object
(not copies of objects) only once.
Alternatively, if a method needs to return an object of a
particular class, you can use the following
methodReturn*
statements. These are macros that
implement the statements to store and return objects
which represent different types or classes, like ints
as Integer objects, doubles
as Float objects,
and so on.
These macros have been superseded in more recent versions of Ctalk, which has the ability to insert the correct return code for any class of object, and many C variables, functions, and expressions. If the compiler doesn’t recognize some particular expression, however, these macros may still be useful.
Remember that these return value functions are implemented as macros and contain their own code block, so you can use them in places where normal functions would cause a syntax error.
methodReturnBoolean (int
i)
Return a Boolean
object that Ctalk can evaluate to TRUE
or FALSE
depending on the value of its argument.
Mostly deprecated; an expression like, "return TRUE"
is equivalent.
methodReturnFalse
Return a Boolean
object that evaluates to FALSE.
Deprecated; using the expression, ‘return FALSE;’ has
the same effect.
methodReturnInteger(int
i)
Return an Integer
object with the value i.
Mostly deprecated; an expression like, "return <int>"
is equivalent.
methodReturnLongInteger(int
l)
Return a LongInteger
object with the value l.
Mostly deprecated; an expression like, "return <longlongint>|<longint>"
is equivalent.
methodReturnNULL
Returns the C value NULL.
Deprecated; simply use the
expression, ‘return NULL;’ instead.
methodReturnObject(object)
Return object. Deprecated; the expression, ‘return object’ has the same effect.
methodReturnObjectName(objectname)
Return the object referred to by objectname. Also
deprecated; like methodReturObject
, above, the
expression, ‘return object’ has the same effect.
methodReturnSelf
Returns the method’s receiver, self
. Slightly
deprecated; simply using the statement, “return
self
,” has the same effect.
methodReturnString(char *
s)
Return a String
object with the value s.
Mostly deprecated; an expression like, "return <char
*>|<string constant>"
is equivalent.
methodReturnTrue
Return a Boolean
object that evaluates to TRUE.
Deprecated; use the expression, ‘return TRUE;’ instead.
The macros that return objects use the __ctalkRegisterUserObject function to keep track of method’s return values, and, if necessary, other objects that the method creates. See __ctalkRegisterUserObject.
Next: Variable arguments, Previous: Templates, Up: Methods [Index]