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.
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...
Constants
In Cangjie, variable definition uses the following structure: Modifier Variable name: Variable type = Initial value Mutability modifiers:...
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...
Values
Cangjie has various value types, including strings, integers, floats, booleans, etc. Here are a few basic examples. Strings can be added ...
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...
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...
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...
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...
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...
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...
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...
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...
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...
Function Overloading
Cangjie supports function overloading in multiple scenarios. Overloaded functions have the same name but differ in the number or type of ...
Match
Match expressions use pattern matching in the following ways: | is used to connect multiple patterns. The wildcard pattern is defined by ...
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 ...
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...
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 ...
Iterators
To implement iteration functionality for user-defined type, it has to implement Iterable interface. User-created class that implements It...
Variadic Functions
Variadic functions can be called with any number of trailing arguments. This function will take any number of ints as arguments. Cangjie ...
Closure
Cangjie supports closures. You can only enclose immutable variables, this can naturally be by-passed by mutating mutable variables within...
Recursion
Cangjie supports recursive functions. Simple function calculating factorial of a number. We can declare function inside functions, and th...
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...
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...
Structs
The following examples illustrate how to define structs. The Rectangle struct has 2 member variables, cangjie supports overloading for co...
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...
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...
Class Inheritance
Like most programming languages that support the class type, Cangjie supports class inheritance. If class B inherits class A, class A is ...
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...
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...
Generic Functions
Generic functions can by created by adding type parameter after the functions name. To enforce certain type constraints you use where syn...
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...
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...
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...
Path
The fs package provides functions to parse and construct file paths in a way that is portable between operating systems (hopefully); dir/...
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...
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 ...
Sorting
There are two types of sorting in cangjie, Stable sort and Unstable sort. Stable sort ensures that the sequence or equal elements remain ...
JSON
The json package can be imported as encoding.json. It supports processing of JSON data and conversion among String, JsonValue and DataMod...
Time
The time package provides time-related types, including date and time, time interval, monotonic time, and time zone, and provides functio...
Random
The random packages generates pseudo random numbers. The random class sets a seed in which it uses to generate random numbers, instance w...
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...
Atomic Operations
An atomic operation is an indivisible and uninterruptible unit of work that either completes entirely or has no effect, guaranteeing data...
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...
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...
Mutexes
A Mutex (mutual exclusion) is a synchronization primitive that acts like a lock, ensuring only one thread can access a critical section o...
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...
TCP
Cangjie supports transport protocols, including TCP. For more information see the official docs Set up port for communication. Function t...
UDP
Cangjie also supports UDP transmision protocol. For more information see the official docs Set the server port. Function to create UDP se...
HTTP
The packet headers of requests and responses contain many fields, which are not described here. For more information see the official doc...
Exceptions
In Cangjie, exception types are class types, and there are two base exception classes: Error and Exception. Error and its subclasses desc...
Effect Handlers
Effect handlers are non-local control operations that allow programs to pause and resume execution. They are similar to exceptions but ex...
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...