In the Cangjie language, generics and type extension are two highly productive features. If multi-paradigm support is the soul of Cangjie, then generics and the extension mechanism are the tools that truly boost development efficiency and code reusability.
During my actual development experience with HarmonyOS 5 applications, these features helped me reduce duplicate code significantly while making the system architecture clearer and more flexible. In this article, we’ll explore these two powerful features from a practical perspective.
Generic Programming: Write Once, Use Everywhere
Generics allow the use of type parameters when defining classes, interfaces, and functions—without binding to a specific type. This abstraction:
- Improves code reusability
- Ensures type safety at compile-time
📌 Application Scenarios of Generics in Cangjie
Scenario | Example |
---|---|
Generic Class |
Array<T> , Map<K, V>
|
Generic Function | func concat<T>(lhs: Array<T>, rhs: Array<T>) |
Generic Interface | interface Repository<T> |
Generic Extension | extend generic type |
Example: Generic Function to Concatenate Arrays
Suppose we want to concatenate two arrays of any type. Traditionally, you’d write separate functions for each type. With Cangjie generics:
func concat<T>(lhs: Array<T>, rhs: Array<T>): Array<T> {
let defaultValue = if (lhs.size > 0) {
lhs[0]
} else if (rhs.size > 0) {
rhs[0]
} else {
return []
}
let newArr = Array<T>(lhs.size + rhs.size, item: defaultValue)
newArr[0..lhs.size] = lhs
newArr[lhs.size..lhs.size+rhs.size] = rhs
return newArr
}
main() {
let a = [1, 2, 3]
let b = [4, 5, 6]
println(concat(a, b)) // Output: [1, 2, 3, 4, 5, 6]
}
-
T
is a generic type parameter—it can beInt
,String
, or any custom object. - With type inference, developers don’t even need to specify
T
manually in most cases.
Generic Constraints: Precise Control of Type Capabilities
Sometimes, you want to restrict the generic type to support certain operations (e.g., comparison, sorting). Cangjie supports this via the where
clause.
func lookup<T>(element: T, arr: Array<T>): Bool where T <: Equatable {
for (let e in arr) {
if (element == e) {
return true
}
}
return false
}
-
T <: Equatable
meansT
must implement theEquatable
interface.
Syntax | Meaning |
---|---|
<: |
Subtype constraint |
Equatable |
Type must support equality comparison |
💡 Practical Benefit: Ensures correctness before runtime—no more hidden type errors.
Type Extension: Non-Intrusively Enhance Existing Types
Type extension allows you to add methods or interfaces to existing types without modifying their source code. This promotes modularity and maintainability.
Example: Add Method to String Type
Let’s say we want every String
to print its length:
extend String {
func printSize() {
println(this.size)
}
}
main() {
"HarmonyOS".printSize() // Output: 9
}
-
extend
begins the type extension. -
this
refers to the current object. - Extended methods behave like native methods.
Advanced Use: Implementing Interfaces via Extension
Cangjie even lets types implement new interfaces through extensions:
sealed interface Integer {}
extend Int8 <: Integer {}
extend Int16 <: Integer {}
extend Int32 <: Integer {}
extend Int64 <: Integer {}
main() {
let a: Integer = 123
println(a)
}
-
sealed
means the interface can only be implemented within the same package. - Built-in types like
Int32
can now participate in interface-based polymorphism.
🔍 Summary Table: Cangjie’s Generics & Extension Features
Feature | Description | Example Use Case |
---|---|---|
Generic Class/Interface | Create reusable, type-safe structures |
Array<T> , Repository<T>
|
Generic Function | Abstract operations across types |
concat<T> , lookup<T>
|
Generic Constraint | Restrict generics to required interfaces | where T <: Equatable |
Type Extension (Method) | Add methods to existing types | String.printSize() |
Type Extension (Interface) | Add interface implementations | Int32 <: Integer |
Conclusion
During HarmonyOS 5 development, mastering the generics and type extension mechanisms of the Cangjie language brings three key benefits:
- ✅ Reusable logic through generics, reducing boilerplate code.
- ✅ Safe and robust design with compile-time type checks.
- ✅ Open-closed architecture, where new behaviors are added without altering the original codebase.
If you aim to write clean, elegant, and scalable code, then generics and extensions are essential tools in your Cangjie toolbox.
Ready to level up your HarmonyOS 5 development? Start experimenting with these features today—and write code that’s truly built to last.
Top comments (0)