Mobile Development 11 min read

Cangjie Programming Language: Features and Integration with ArkTS

Cangjie is a general-purpose programming language designed for efficient application development, offering lightweight runtime, user threads, and seamless ArkTS interoperability.

JD Tech Talk
JD Tech Talk
JD Tech Talk
Cangjie Programming Language: Features and Integration with ArkTS

Cangjie is a general-purpose programming language designed for application development, balancing development efficiency and runtime performance. It offers several key advantages:

1. Efficient Programming : Concise syntax supporting multiple paradigms, including interpolated strings, primary constructors, Flow expressions, match, if-let, while-let, and re-exports to reduce redundancy and improve productivity.

2. Lightweight Runtime Library : Standard library packages (except core) can be linked on-demand, with core package under 1MB.

3. Lightweight User Threads : Uses user-space thread model with 8KB memory overhead per thread, no kernel transitions, and nanosecond-level switching with dynamic stack resizing.

4. Lightweight Objects : Each object consumes only one machine word for type information, no field counting, and uses forwarding tables instead of forwarding pointers for GC.

5. Type Safety : As a statically-typed language with strong type checking at compile time, it reduces runtime risks and offers powerful type inference to minimize type annotations.

Quick Environment Setup : Download the DevEco Cangjie plugin from the HarmonyOS developer website, install it in DevEco Studio, and create projects similar to ArkTS.

Key Features :

- Flow Expressions : Two operators - |> for data flow (e.g., gen |> forEach{a => println("${a}")} ) and ~> for function composition (e.g., var fg = f ~> g ).

- Variable Arguments : When the last non-named parameter is an Array type, it can be used as variable arguments without special syntax.

- Extensions : Add member functions, operator overloads, properties, or implement interfaces to classes without inheritance or decorator patterns.

- if-let and while-let : Pattern matching expressions for conditional control and loops, similar to ArkTS but with added convenience.

- Threads : Use spawn{} to create threads and Future<T> to get results. Synchronization mechanisms include atomic operations, ReentrantMutex, and synchronized blocks.

ArkTS Interoperability : Use @Interop[ArkTS] annotation on classes, interfaces, and functions to generate ArkTS declaration files and interop layer code. Generate API files via right-click in DevEco Studio, then import and use in ArkTS.

Future Planning : Cangjie and ArkTS are parallel development paths in HarmonyOS. Cangjie addresses performance issues in ArkTS APIs and simplifies thread management, making it valuable for building robust HarmonyOS applications like JD Finance.

Mobile DevelopmentHarmonyOSSynchronizationprogramming languagetype safetyArkTS interoperabilityCangjieFlow expressionsuser threads
JD Tech Talk
Written by

JD Tech Talk

Official JD Tech public account delivering best practices and technology innovation.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.