@C Annotation
The @C annotation tells the compiler to use C-compatible calling convention and memory layout. It is primarily used to modify struct types, ensuring their memory layout is exactly consistent with C structs, so data can be safely passed between C and Cangjie.
Structs modified with @C have strict restrictions: member types must satisfy the CType constraint, cannot implement interfaces, cannot have generic parameters, and cannot be captured by closures. These restrictions ensure binary compatibility with C.
@C can also modify top-level functions, making them callable by C code. Parameters and return types of such functions also need to satisfy the CType constraint. When calling Cangjie functions modified with @C, it also needs to be in an unsafe context.
Structs modified with @C automatically satisfy the CType constraint and can be used as parameter or return types for foreign functions.
The example below defines an @C-modified Point struct and an @C-modified addPoints function. Try removing the @C from addPoints to observe the compilation error when calling it.