Extension Overview

Extensions add new functions (but not data!) to existing types that are visible in the current package, without breaking their encapsulation. Any Cangjie type aside from function types, tuple types, and interfaces can be extended.

An extension can add any or all of the following to the type that it extends:

  • Member functions
  • Operator overloads
  • Member properties
  • Interface implementations

At the same time, the requirement to preserve the encapsulation of the original type imposes the following restrictions on extensions:

  • An extension cannot add member variables to a type.
  • An extension cannot add abstract member functions or properties to a type.
  • Members added by extensions cannot hide, override or redefine the existing members of a type, nor can they be overridden/redefined in its subtypes.
  • Extensions cannot access members modified by private in the extended type.

Extensions can be classified into direct extensions and interface extensions based on whether they make the extended type implement new interfaces. A direct extension does not include extra interfaces. An interface extension includes interfaces and can be used to add new functions while also implementing those interfaces, thus enhancing abstraction and flexibility.