Location>code7788 >text

Comparison of ArkTS and Cangjie's Characteristics and Cases

Popularity:890 ℃/2024-10-11 08:51:30

ArkTS and Cangjie are two different programming languages, each with unique characteristics and design purposes.

ArkTS Features

ArkTS is a TypeScript-based programming language specially designed for Hongmeng application development. It retains most of the syntactic features of TypeScript, optimized and customized for the Hongmeng system.The main advantages of ArkTS include:

  • Static type checking: type-checking at compile time to catch errors in advance .
  • Better IDE support: Provide better autocompletion, navigation and refactoring .
  • Easy to maintain and understand: Type annotations and compile-time checks make code easier to understand and maintain.

ArkTS also imposes restrictions on some features of TypeScript to ensure better performance and development correctness, for example:

  • prohibition on useanytypes, forcing the use of static types .
  • Prohibit changes to object layout at runtime, such as adding new properties or deleting existing ones .
  • Restricting the semantics of operators, e.g. the unary operator+Only works on numeric types .

Cangjie characteristics

Cangjie is Huawei's self-developed programming language for full-scene intelligence, featuring native intelligence, full-scene support, high performance, and strong security. Cangjie is designed with modern software development needs in mind and offers the following features:

  • multiparadigm programming: A blend of functional, imperative, and object-oriented programming.
  • genre inference: Ease the developer's workload on type annotation .
  • Simple and efficient syntax: Reduce code redundancy and improve development efficiency .
  • Built-in syntactic sugar and macro functions: Provides the ability to build domain-specific languages (DSLs).

Cangjie also places special emphasis on safety and performance:

  • safety: To realize the vision of "coding as security" by integrating security concepts into language design.
  • high performance: Fully concurrent GC and lightweight threaded design for superior performance support .

Application Scenarios and Performance Efficiency

ArkTS is mainly applied to the development of UI and business logic of Hongmeng system, which is suitable for the development of applications that need to utilize the characteristics of Hongmeng. On the other hand, Cangjie is targeted at full-scenario application development, including but not limited to mobile devices, embedded devices and server-side applications. In terms of performance and efficiency, ArkTS improves runtime performance by restricting certain TypeScript features, while Cangjie achieves high performance through full-stack compilation optimization and runtime optimization.

To summarize, ArkTS is a programming language optimized for the Hongmeng system on the basis of TypeScript, which is suitable for Hongmeng application development; while Cangjie is a new generation programming language designed by Huawei for the development of full-scenario intelligent applications, which is characterized by high performance and strong security. Both have their own focus, developers can choose the right programming language according to the project needs and scenarios.

Since ArkTS is based on TypeScript and Cangjie is a new programming language from Huawei, there may be big differences in syntax and features between the two. Below are some code examples of both to show how they differ in realizing the same functionality.

ArkTS Example

Assuming we're using ArkTS to create a simple Todo app, we might write it like this:

// 
class TodoItem {
  id: number;
  title: string;
  isCompleted: boolean;

  constructor(id: number, title: string) {
     = id;
     = title;
     = false;
  }

  complete() {
     = true;
  }
}

// 
class TodoApp {
  items: TodoItem[];

  constructor() {
     = [];
  }

  addTodo(title: string) {
    const newItem = new TodoItem(, title);
    (newItem);
  }

  completeTodo(id: number) {
    const item = (item =>  === id);
    if (item) {
      ();
    }
  }
}

// 
const app = new TodoApp();
("Learn ArkTS");
("Try out HarmonyOS");

(0);

for (const item of ) {
  (`${} - ${ ? "Completed" : "Pending"}`);
}

Cangjie Example

For the same Todo application, the code using the Cangjie language might look like the following:

//
class TodoItem {
  var id: Int;
  var title: String;
  var isCompleted: Bool;

  func constructor(id: Int, title: String) {
     = id;
     = title;
     = false;
  }

  func complete() {
     = true;
  }
}

//
class TodoApp {
  var items: List<TodoItem>;

  func constructor() {
     = new List<TodoItem>();
  }

  func addTodo(title: String) {
    let newItem = new TodoItem((), title);
    (newItem);
  }

  func completeTodo(id: Int) {
    let item = (item => == id);
    if (item != null) {
      ();
    }
  }
}

//
func main() {
  let app = new TodoApp();
  ("Learning the Cangjie Language");
  ("Experience the Hongmeng system");

  (0);

  for (item in ) {
    print(`${} - ${ ? "done" : "unfinished"}`);
  }
}

Please note that the above code for Cangjie is a hypothetical example, as the specific syntax and features of Cangjie may vary, while the code for ArkTS is closer to the regular usage of TypeScript.

The two examples show how to define a class, its methods, and how to manipulate them in two different languages. the ArkTS example uses TypeScript's class syntax and array methods, while the Cangjie example shows the possible ways of defining a class and calling its methods, with reference to the actual documentation of the Cangjie language for the exact syntax.

ultimate

Like the previous introduction, the two languages have their own merits, according to business scenarios to choose the right one, is the best, welcome to pay attention to Vigo love programming, the growth of the road together side by side.