Reflection
Reflection allows programs to inspect type information at runtime and dynamically access members. Cangjie provides reflection capabilities through the TypeInfo type, which is the core type for reflection and records metadata for any type.
There are three ways to obtain TypeInfo: through an instance object, through a generic parameter, or through a type name string. TypeInfo.of(obj) returns the runtime type information of an object, while TypeInfo.of<T>() returns static type information for type T. For class types, TypeInfo.of(obj) returns ClassTypeInfo, which provides more class-specific reflection interfaces.
Reflection can access public members of a type, including static variables, instance variables, properties, and methods. Through getStaticVariable and getInstanceVariable, you can find member variables by name, then use getValue and setValue to read and write their values.
public members; private and protected members are not visible.The code on the right demonstrates reading and modifying the instance variable value and static variable step of the Counter class through reflection.
Try modifying the parameter of setValue and observe the output changes.