DEV Community

Rory-lhj
Rory-lhj

Posted on • Edited on

HarmonyOSNext-The Basic Composition of a Cangjie Program

This article systematically introduces the basic components of a program in the Cangjie programming language, covering the definition of the main function, the use of packages and modules, variable types, scope, and code structure principles. It aims to help developers understand the overall structure of Cangjie programs and adopt best practices in software design. Understanding these components is essential for writing clean, maintainable, and modular code in Cangjie.

๐Ÿ”‘ Keywords

  • Program entry point
  • Main function
  • Packages and modules
  • Variable types and scope
  • Value types and reference types
  • Code structure and specifications
  • Mutability and visibility control
  • Top-level declarations and type definitions I. Program Entry Point and Main Function In Cangjie, the main function serves as the entry point of a program and is responsible for initializing runtime logic and signaling exit status.

1.1 Definition of the Main Function

main(): Int64 {
    return 0
}
Enter fullscreen mode Exit fullscreen mode

This simple main function returns 0, indicating a successful termination. Like in other statically typed languages, return values from main() help indicate success or failure to the operating system or invoking shell.

1.2 Parameters and Return Values
The main function can optionally receive arguments via an Array parameter.

The return type is typically Int64 but may also be Unit if no explicit exit code is required.

main(args: Array<String>): Int64 {
    println("Received ${args.length} arguments.")
    return 0
}
Enter fullscreen mode Exit fullscreen mode

โš ๏ธ Arguments are passed when executing the program from a shell or script environment.

II. Packages and Modules
Modular programming in Cangjie is achieved using packages and modules, allowing better code organization and reusability.

2.1 Defining Packages
Use the package keyword at the top of the file to declare a package:

package cjcDemo
Packages are typically structured based on project hierarchy and logical function grouping.

2.2 Importing Modules
Use the import keyword to include other modules or libraries:

import std.math.*
import utils.io.FileReader
๐Ÿ” Wildcard imports are supported (*), but it's recommended to import only required symbols to maintain clarity and avoid naming conflicts.

III. Program Structure and Top-Level Scope
Cangjie program files use the .cj extension. The top-level scope allows declarations of global constants, functions, and types that are accessible across the file or package.

3.1 Global and Local Scope


let globalVar = 2023

func globalFunc() {
    println(globalVar)
}

main(): Int64 {
    globalFunc()
    return 0
}
Enter fullscreen mode Exit fullscreen mode

Global variables are accessible throughout the file or module.

Local variables declared within a function or block are only valid within that context.

3.2 Nested Scope and Shadowing

let x = 10
func testScope() {
    let x = 5  // Shadows outer x
    println(x) // Prints 5
}
Enter fullscreen mode Exit fullscreen mode

Inner variables with the same name override outer definitions within their local scope.

IV. Variable Definition and Usage
Cangjie variables are defined with strong typing and explicit mutability.

4.1 Modifiers
let โ€“ immutable constant

var โ€“ mutable variable

public / private โ€“ visibility control

static โ€“ shared at the type level

Example:

cj

let a: Int64 = 20
var b: Int64 = 12
b = 23
println("${a} ${b}")
Output: 20 23
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Best practice: use let by default unless mutation is required.

V. Value vs Reference Types
Cangjie distinguishes between value types (e.g., Int64, Bool) and reference types (e.g., class, struct with heap semantics). Understanding this distinction is important for performance tuning and memory safety.

cj

struct Point {
    x: Int64
    y: Int64
}

class User {
    name: String
    score: Int64
}

main(): Int64 {
    let p = Point(x: 3, y: 4)
    let u = User(name: "Alice", score: 95)

    println("Point: (${p.x}, ${p.y})")
    println("User: ${u.name} scored ${u.score}")
    return 0
}

Enter fullscreen mode Exit fullscreen mode

struct creates value types (copied on assignment).

class creates reference types (shared references).

By mastering these fundamentals, developers can confidently build scalable and maintainable Cangjie programs across various domains such as systems programming, education, and cloud-native development.

Top comments (0)