Types Declaration

Types can be used at four places:

Normally, the type information is completely ignored, and can be regarded purely as documentation. However, when the basic type of a function is declared, then a more strict type checking will be enforced. That means that the type of all arguments must be defined. And, the variables can only be used to store values of the declared type. The function @dfn{call_other} is defined to return an unkown type, as the compiler can't know the type. This value must always be casted (when strict type checking is enabled). Casting a type is done by putting the type name inside a pair of '(' and ')'.

An example when querying the short description of an object:

(string)call_other(ob, "short");

When a function is compiled with strict type testing, it can only call other functions that are defined. If they are not yet defined, prototypes can be defined.

string func(int arg);

Note the ';' instead of a body to the function. All arguments must be givenby names, but does not have to have the same names as in the real definition. All types must of course be the same.

There is currently a bug (3.0.36) that recursive calls can not be done if the function is not also defined by a prototype. That is because a function is not really defined until the whole function has been compiled. Don't rely on this behaviour, which will hopefully soon be fixed. A dangerous effect is that it is possible to redefine efun's, and let the new definition call the old before it is replaced. Such code will break sooner or later.

If an efun is to be redefined to get some new enhancements, always define a new with a new name.

There are two kinds of types. Basic types, and special types. There can be at most one basic type, but any number of special types. The strict type checking is only used by the compiler, not by the runtime. Hence, it is actually possible to store a number in a string variable even when strict type checking is enabled.

Why use strict type checking? It is really recommended, because the compiler will find many errors at compile time, which will save a lot of hard work. It is in general much harder to trace an error occuring at run time. I recommend, that when a wizard is having problem with an object and wants help, that he first must make all functions have declared types.

The basic types can be divided in to groups. Those that are referenced by value, and those that are referenced by address. The types int and string are always representing different entities. But the type object is a pointer to an object. If a value of this type is assigned to a variable or passed as argument, they will all point to the same object. The same goes for arrays. That means that if the value of an element in an array is changed, then it can modify all other variables pointing to the same array. Changing the size of the array will always allocate a new one, though. The comparation operator, ==, will compare the actual value for the group of types above. But for arrays and objects, it will simply check if it is the same object (or array). That has the very important implication that the expression ({}) == ({}) will always evaluate to false becaus the array construction operator-pair, ({ }) always generates a new array.

Basic types