Cangjie for HarmonyOS: Fast Setup, Key Features, and ArkTS Interop
This article introduces Cangjie, a lightweight, high‑performance programming language for HarmonyOS, detailing its core advantages, rapid environment configuration, distinctive language features such as flow expressions, lightweight threads, extensions, synchronization primitives, and demonstrates seamless interoperation with ArkTS, concluding with future development outlook.
1. Introduction to Cangjie
Cangjie is a general‑purpose programming language designed for a wide range of applications on HarmonyOS, balancing development efficiency with runtime performance. Its main advantages include concise syntax, a lightweight runtime library (core <1 MB), user‑level lightweight threads, minimal object overhead, and strong static type safety with powerful type inference.
2. Quick Environment Setup
2.1 Download
Visit the HarmonyOS developer site, locate the DevEco Cangjie plugin in the download center, and download the latest version.
2.2 Installation
Open DevEco Studio, go to Settings → Plugins, choose “Install from Disk”, select the downloaded plugin, and restart the IDE.
2.3 Project Creation
Create a new project just like an ArkTS project.
3. Key Language Features
3.1 Flow Expressions
Flow operators enable pipeline‑style processing. The infix operator |> passes the left‑hand value to the right‑hand function, while ~> composes single‑argument functions.
let gen: Array<Int64> = []
gen |> forEach{a: Int64 => println("${a}")}
func split(words: Array<String>, separator: Rune): Array<String> {
words |> map { text => text.split(String(separator), removeEmpty: true) }
|> flatten
|> collectArray
}3.2 Variable‑Length Parameters
If the last non‑named parameter is an Array, it acts as a variadic argument without special syntax.
func sum(arr: Array<Int64>) {
var total = 0
for (x in arr) { total += x }
return total
}
main() {
println(sum()) // 0
println(sum(1, 2, 3)) // 6
}3.3 Extensions
Cangjie allows extending classes and interfaces without inheritance or decorators, adding methods, operators, properties, or implementing interfaces while preserving encapsulation.
// Extend Int64 with row/col properties
extend Int64 {
public prop r: Index { get() { Index.Row(this) } }
public prop c: Index { get() { Index.Col(this) } }
}
// Extend a type with a static factory method
extend Expression {
static func fromTokens(tokens: List<Token>): Result<Expression, String> {
match (expressionFunc(tokens).map {t => t[0]}) {
case Some(e) => Ok(e)
case None => Err("Invalid Expression!")
}
}
}3.4 if‑let and while‑let
Pattern‑matching control flow similar to Rust’s if let and while let constructs.
main() {
let result = Option<Int64>.Some(2023)
if (let Some(value) <- result) {
println("Success: ${value}")
} else {
println("Failure")
}
} public static func fromJson(r: JsonReader): FunctionCall {
while (let Some(v) <- r.peek()) {
// process JSON tokens
}
return FunctionCall(...)
}3.5 Threads
Lightweight user‑mode threads are created with spawn{}, returning a Future<T> for result retrieval.
let future = spawn { task() }
future.get() // wait for completion3.6 Synchronization
Cangjie provides atomic types, re‑entrant mutexes, and a synchronized keyword that automatically locks and unlocks a ReentrantMutex.
let num = AtomicInt64(0)
let lock = ReentrantMutex()
synchronized(lock) { num++ }3.7 Interoperability with ArkTS
By annotating Cangjie symbols with @Interop[ArkTS], the compiler generates ArkTS declaration files and interop glue code, enabling seamless calls from ArkTS.
@Interop[ArkTS]
public func sub(a: Int64, b: Int64): Int64 { return a - b }
@Interop[ArkTS]
public class CjDemo {
public let name: String
@Interop[ArkTS, Invisible]
public var id: Float64 = 1.0
public init(str: String) { name = str }
public func add(a: Int64, b: Int64): Int64 { return a + b }
}Generated ArkTS declarations:
export declare class CjDemo {
name: string
add(a: number, b: number): number
foo(): number
}
export declare interface CustomLib {
sub(a: number, b: number): number
CjDemo: {new (str: string): CjDemo}
}4. Future Roadmap
HarmonyOS currently supports both ArkTS and Cangjie, and both are expected to evolve in parallel. Cangjie’s performance‑oriented design and richer concurrency primitives make it a valuable addition for building robust, high‑performance financial applications on HarmonyOS.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JD Cloud Developers
JD Cloud Developers (Developer of JD Technology) is a JD Technology Group platform offering technical sharing and communication for AI, cloud computing, IoT and related developers. It publishes JD product technical information, industry content, and tech event news. Embrace technology and partner with developers to envision the future.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
