Cangjie is a modern programming language designed for intelligent, cross-secnario development. It integrates deeply with the HarmonyOS ecosystem, emphasizing native intelligence, full-scenario adaptabillity, high performance, and strong security.
Key Design Goals
Native Intelligence Cangjie incorporates an AgentDSL programming framework, blending natural language with code. It supports multi-agent collaboration, simplifying symbolic expressions and enabling flexible composition of patterns for intelligent application development.
Full-Scenario Adaptability A lightweight and scalable runtime, combined with a modular architecture, ensures Cangjie runs efficiently even on devices with limited memory. Metaprogramming and eDSL (embedded Domain-Specific Language) capabilities allow developers to create declarative, domain-specific solutions.
High Performance Cangjie features the first fully concurrent garbage collector (GC) for terminal scenarios, resulting in smoother application threads and faster response times. Lightweight threading improves concurrency with reduced overhead.
Strong Security Security principles are embedded into the language design. Developers are guided to focus on business logic rather than defensive coding, reducing vulnerabilities and making secure coding the default.
Programming Paradigms
Cangjie supports multiple programming paradigms—procedural, object-oriented, and functional—allowing developers to choose the best approach for their tasks. It includes modern language features like declarative syntax, syntactic sugar, and the ability to define Domain-Specific Languages (DSLs) for specific use cases.
Core Features
- Value Types, Classes, and Interfaces: Support for structured data using value types, classes, and interfaces.
- Generics: Type-safe abstractions using generic programming.
- Algebraic Data Types (ADTs) and Pattern Matching: Powerful constructs for modeling complex data and control flow.
- First-Class Functions: Functions can be assigned to variables, passed as arguments, and returned from other functions.
Classes and Interfaces in Practice
Cangjie allows object-oriented programming via class and interface. Single inheritance is enforced for classes—each class can have only one parent class but can implement multiple interfaces. All classes implicitly implement the Any interface, and every class extends Object.
The open modifeir controls inheritance: a class marked open can be subclassed, and an open member function can be overridden by subclasses.
Consider the following example:
open class A {
let x: Int = 1
var y: Int = 2
open func f() {
println("function f in A")
}
func g() {
println("function g in A")
}
}
interface I1 {
func h1()
}
interface I2 {
func h2()
}
class B <: A & I1 & I2 {
override func f() {
println("function f in B")
}
func h1() {
println("function h1 in B")
}
func h2() {
println("function h2 in B")
}
}
main() {
let o1: I1 = B()
let o2: A = A()
let o3: A = B()
o1.h1() // "function h1 in B"
o2.f() // "function f in A"
o3.f() // Dynamic dispatch: "function f in B"
o3.g() // "function g in A"
}
Here, class A is declared open so it can be inherited. Function f is also open and is overridden in B. The call o3.f() uses dynamic dispatch based on the actual object type.
Interfaces can also inherit from multiple parent interfaces:
interface I1 {
func f(x: Int): Unit
}
interface I2 {
func g(x: Int): Int
}
interface I3 <: I1 & I2 {
func h(): Unit
}
To implement I3, a class must provide implementations for f, g, and h.