Function Definitions
function-definition: export type id ( parameter-list ) function-propertiesopt function-body
export:
public
package
private parameter-list: parameter parameter-list, parameter function-body: { statement* } function-properties: : function-property+ function-property: thiscall widening converter inline encrypted nonfglue intrinsic referred pure no return method-only-property: abstract null==expr require super
Example:
public int funcName(int a, int b) : abstract {
statements
}
package void funcName(StrBuf buf) {
statements
}
Export
-
public
Able to be invoked from anywhere. -
package
Only able to invoked by code within the same directory (not including subdirectories). -
private
Only able to invoked in the same file.
Function Properties
Function definitions can list various properties to request special treatment or signal aspects of their behavior.
-
thiscall
These explicitly denote low-level calling conventions to use (e.g., stuff like what’s passed in on the stack vs special registers) and IIRC for the most part are really only relevant for `import` functions/methods (i.e., tied to dlls)
-
widening converter
Registers function as a means of implicit conversion between types (namely its formal arg’s type and its return type).We STRONGLY discourage adding your own widening converters as it makes code incredibly hard to read, and impossible to inspect using alt-i. -
nonfglue
Bindings required to stick C++ code to CM code. Bindings support bidirectional communication, inheritance and automatic declarations.
-
inline
Request inlining of all calls to the function. Use with caution since overuse may lead to very large function bodies.
-
intrinsic
Specifically use an intrinsic function found in the imported file.
-
referred
Turn off all warnings that the function appears to be unused.
-
pure
Signals that the function is referentially transparent and that calls to it may be optimized or precomputed. A function is referentially transparent if the return value depends only on the parameters and not on any global state and the function lacks side effects. Only meant for the compiler developers.
-
noreturn
Makes the function never return to the caller – it always throws an exception or performs some other form of non-local exit.
Currently optional.
Method only properties
If a function is inside a class, these additional properties can be added:
-
abstract
Abstract method (subclass responsibility). Subclasses are required to override this method.
-
null=expr
“field access” (i.e., `owner.something(…)`) calls to this expression when `owner` is null.
If method/function’s return type is non-null, the result of `expr` is returned.
-
require super
Any methods that override the one declared with this property must call `super(…)` as part of their overriding implementation.
Aliases
An alias
can be used to make a short-hand to a function definition.
function ( parameter-list ): type
This is often used with callback functions. For example, in cm/core/part/partHooks.cm :
/** * Raw hooks list. */ private alias completeListHook = function (Part[] list, Space space):Part[]; /** * Raw part hooks. */ private completeListHook{} completeListHooks : keep; /** * Append a complete part list hook. */ public void appendRawPartListHook(completeListHook hook) { if (hook) { if (!completeListHooks) init completeListHooks(); completeListHooks << hook; } }
Function Object
In CM, any function can be cast into a Function
object and vice versa:
private int normalFunction(int a, str b) { return a; } { Function functionObject = function normalFunction; if (function(int, str):int backToNormal = functionObject.:function(int, str):int) { pln(backToNormal(5, "")); } }
Comments
0 comments
Please sign in to leave a comment.