foreign Declarations
The foreign keyword is used to declare an external C function with only a function signature and no function body—the linker resolves the actual implementation at compile time. Calls to foreign functions must be made within an unsafe block because C code may perform unsafe operations.
Cangjie has explicit type mappings with C basic types: Int32 corresponds to int32_t, Int64 corresponds to int64_t, and Unit corresponds to void. Parameters and return types must conform to C and Cangjie type mapping requirements.
Functions declared with foreign do not support named parameters and default values, but support variadic parameters (indicated by ...). The @C annotation can be omitted on foreign declarations, as the compiler will automatically infer it.
foreign can only modify function declarations, not variables, classes, or interfaces. Functions modified by foreign cannot have a function body.
The example below declares C standard library functions rand(), abs(), and srand(), and calls them within unsafe blocks. Try adding a call to srand() to set the random seed.