Learn Cangjie

Master the modern programming language step by step.

Choose a Lesson to Begin

Select any lesson below to start learning Cangjie programming. Each lesson builds upon the previous ones, but you can jump to any topic that interests you.

1

Hello World

This first program prints "Hello World". Your main function does not need the func modifier. To run the program, copy the code to a .cj f...

2

Constants

In Cangjie, variable definition uses the following structure: Modifier Variable name: Variable type = Initial value Mutability modifiers:...

3

Variables

You can declare variables with one of two modifiers: var or let: var makes the variable mutable, while let makes it immutable. Cangjie ca...

4

Values

Cangjie has various value types, including strings, integers, floats, booleans, etc. Here are a few basic examples. Strings can be added ...

5

Functions

This function takes 2 integers and returns their sum as an integer. Please note explicit returns are not required i.e last expression wil...

6

Multiple Return Values

If you need to return multiple values from a function you can use tuples, and pattern matching. Return type of this function is a tuple c...

7

If Else

Branching with if and else in Cangjie is straight-forward. Basic example. if statement without an else. You can use logical operators lik...

8

For and while loops

Cangjie has both while and for loops. While the condition enclosed remains true, the loop runs for loop => j iterates 0 through to 3 exc...

9

Range

A range is used to represent a sequence with a fixed step and is denoted by Range<T>. The for-in expression can be used to traverse a ran...

10

Arrays

Arrays are declared as Array<T> where T represents the type. T can be any type. Arrays are a reference type variables C is initialised wi...

11

Strings and Runes

Strings in Cangjie are arrays of UInt8 underneath. Runes can represent any character in Unicode set and are converted from UInt8. Type in...

12

Strings

The string type is denoted by String and is classified into three types: single-line, multi-line, and multi-line raw string literals. \ c...

13

Enums

Enums in cangjie are preceded by the enum keyword, and define all possible values of the type. When match is used on an enum, it must cov...

14

Function Overloading

Cangjie supports function overloading in multiple scenarios. Overloaded functions have the same name but differ in the number or type of ...

15

Match

Match expressions use pattern matching in the following ways: | is used to connect multiple patterns. The wildcard pattern is defined by ...

16

Hashmaps

HashMaps are included in the collection package, and must be imported to be used. HashMap is a reference type. When a HashMap is used as ...

17

Hash Sets

The HashSet type is part of the collection package. It can be used to create sets that contain elements of a type that implements Hashabl...

18

Range Over Builtin Types

If a type implements Iterable interface, we can range over it. All data structures in std.collection implement that interface. Therefore ...

19

Iterators

To implement iteration functionality for user-defined type, it has to implement Iterable interface. User-created class that implements It...

20

Variadic Functions

Variadic functions can be called with any number of trailing arguments. This function will take any number of ints as arguments. Cangjie ...

21

Closure

Cangjie supports closures. You can only enclose immutable variables, this can naturally be by-passed by mutating mutable variables within...

22

Recursion

Cangjie supports recursive functions. Simple function calculating factorial of a number. We can declare function inside functions, and th...

23

If Let

The if-let expression first evaluates the expression on the right of <- in the condition. If the value matches the pattern on the left of...

24

Options

Option type is basically a box that either contains a value or is empty. It allows for performing computations which may fail. In this ca...

25

Structs

The following examples illustrate how to define structs. The Rectangle struct has 2 member variables, cangjie supports overloading for co...

26

Classes

Classes in cangjie are reference objects. This is one of the crucial differences between a class and a struct in Cangjie as a struct is a...

27

Interfaces

An interface is used to define an abstract type. An interface contains no data but can define a behavior of a type. If a type declares th...

28

Class Inheritance

Like most programming languages that support the class type, Cangjie supports class inheritance. If class B inherits class A, class A is ...

29

Mut Functions

The instance member functions in a struct cannot modify the instance, to solve this, a mut function can be used. By using the mut modifie...

30

Function types

Functions have a type in Cangjie. Therefore we can create variables with function type and pass them to the functions. Function type is c...

31

Generic Functions

Generic functions can by created by adding type parameter after the functions name. To enforce certain type constraints you use where syn...

32

Generic Classes

To create a generic class in Cangjie, place any number of type parameters after the class name in <>. Then you can use your type paramete...

33

Console IO

Input and output from the console is done through std.console. These function calls return an option type because the console may be unab...

34

Reading and Writing to Files

In cangjie we use the classes StringReader and StringWriter to avoid manually manipulating Bytes Here we create ByteArrayStream() and use...

35

Path

The fs package provides functions to parse and construct file paths in a way that is portable between operating systems (hopefully); dir/...

36

Directories

There is a lot of different functions available in Cangjie regarding directories. We only cover a small portion of them here. You can fin...

37

Regex

For all regexes command reference this website This is a string in which we will look for our pattern. Initialize the regex. Let's break ...

38

Sorting

There are two types of sorting in cangjie, Stable sort and Unstable sort. Stable sort ensures that the sequence or equal elements remain ...

39

JSON

The json package can be imported as encoding.json. It supports processing of JSON data and conversion among String, JsonValue and DataMod...

40

Time

The time package provides time-related types, including date and time, time interval, monotonic time, and time zone, and provides functio...

41

Random

The random packages generates pseudo random numbers. The random class sets a seed in which it uses to generate random numbers, instance w...

42

Threads

You can easily create threads in Cangjie using spawn. Example function we will be calling. Suppose we have a function call f(s). Here’s h...

43

Atomic Operations

An atomic operation is an indivisible and uninterruptible unit of work that either completes entirely or has no effect, guaranteeing data...

44

Accessing Thread

Newly created threads in Cangjie can terminate early if the thread that created them terminates. However, you can use the return value of...

45

Thread Variables

You can use ThreadLocal in the core package to create and use thread local variables.each thread can safely access its own thread local v...

46

Mutexes

A Mutex (mutual exclusion) is a synchronization primitive that acts like a lock, ensuring only one thread can access a critical section o...

47

Monitors

Monitor is a built-in data structure that binds a mutex to a single related condition variable (wait queue). Monitor can block a thread a...

48

TCP

Cangjie supports transport protocols, including TCP. For more information see the official docs Set up port for communication. Function t...

49

UDP

Cangjie also supports UDP transmision protocol. For more information see the official docs Set the server port. Function to create UDP se...

50

HTTP

The packet headers of requests and responses contain many fields, which are not described here. For more information see the official doc...

51

Exceptions

In Cangjie, exception types are class types, and there are two base exception classes: Error and Exception. Error and its subclasses desc...

52

Effect Handlers

Effect handlers are non-local control operations that allow programs to pause and resume execution. They are similar to exceptions but ex...

53

Effect Handlers - Resumption

So far, examples we have shown on the previous slide have resumed immediately after handling the effect. However, we do have the option f...