Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fundamentals to Declarations [Draft] #4894

Open
wants to merge 91 commits into
base: docs-restructure
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
2f2a151
docs: rewrite and restructure meet-motoko.md
Demali-876 Feb 5, 2025
35f4442
fix: address feedback
Demali-876 Feb 6, 2025
ea1216f
Update doc/md/meet-motoko.md
Demali-876 Feb 6, 2025
01e2d00
Update doc/md/meet-motoko.md
Demali-876 Feb 6, 2025
fb32218
Update doc/md/meet-motoko.md
Demali-876 Feb 6, 2025
f219cb9
Update doc/md/meet-motoko.md
Demali-876 Feb 6, 2025
a1d1569
Update doc/md/meet-motoko.md
Demali-876 Feb 6, 2025
736d5f1
Update doc/md/meet-motoko.md
Demali-876 Feb 6, 2025
722c6b4
fundamental to pipes draft.
Demali-876 Feb 7, 2025
ad4cc67
updated fundamentals draft.
Demali-876 Feb 7, 2025
506e10a
Update doc/md/meet-motoko.md
Demali-876 Feb 8, 2025
c9db19c
Update doc/md/fundamentals/expressions/operators.md
Demali-876 Feb 8, 2025
868df9d
Update doc/md/fundamentals/basic-syntax/imports.md
Demali-876 Feb 8, 2025
ab6cf78
Update doc/md/getting-started.md
Demali-876 Feb 8, 2025
59f786f
Update doc/md/fundamentals/basic-syntax/imports.md
Demali-876 Feb 8, 2025
4616b08
Update doc/md/fundamentals/basic-syntax/integers.md
Demali-876 Feb 8, 2025
dea3657
Update doc/md/fundamentals/basic-syntax/imports.md
Demali-876 Feb 8, 2025
c268553
Update doc/md/fundamentals/basic-syntax/integers.md
Demali-876 Feb 8, 2025
1dfcaad
Update doc/md/fundamentals/expressions/integers-numbers.md
Demali-876 Feb 8, 2025
a7a058f
Update doc/md/fundamentals/expressions/operators.md
Demali-876 Feb 8, 2025
b0b64f9
Update doc/md/fundamentals/expressions/operators.md
Demali-876 Feb 8, 2025
4a533a5
Update doc/md/fundamentals/basic-syntax/assertion.md
Demali-876 Feb 8, 2025
423d07e
Update doc/md/fundamentals/expressions/operators.md
Demali-876 Feb 8, 2025
31cec71
Update doc/md/fundamentals/expressions/operators.md
Demali-876 Feb 8, 2025
fca412c
Update doc/md/fundamentals/expressions/operators.md
Demali-876 Feb 8, 2025
7765d0b
updates to draft
Demali-876 Feb 8, 2025
733458e
updates to imports.md
Demali-876 Feb 8, 2025
59c6ba2
updates to printing values
Demali-876 Feb 8, 2025
ea8be8d
updates integer
Demali-876 Feb 8, 2025
2aecba0
update to Float
Demali-876 Feb 8, 2025
93cdf13
update to Characters
Demali-876 Feb 8, 2025
480a0a1
update to Text
Demali-876 Feb 8, 2025
04ee43a
update to literals
Demali-876 Feb 8, 2025
2587cdf
update to whitespace
Demali-876 Feb 8, 2025
179e634
updates
Demali-876 Feb 8, 2025
7e03619
Update doc/md/fundamentals/expressions/operators.md
Demali-876 Feb 12, 2025
d488c3d
Update doc/md/fundamentals/expressions/operators.md
Demali-876 Feb 12, 2025
a4d6332
Update doc/md/fundamentals/expressions/operators.md
Demali-876 Feb 12, 2025
1e8428e
Update doc/md/fundamentals/basic-syntax/defining_an_actor.md
Demali-876 Feb 14, 2025
97a59df
Update doc/md/fundamentals/basic-syntax/imports.md
Demali-876 Feb 14, 2025
32604a1
Update doc/md/fundamentals/expressions/operators.md
Demali-876 Feb 14, 2025
2c23a6c
Update doc/md/meet-motoko.md
Demali-876 Feb 14, 2025
db1deda
Update doc/md/fundamentals/basic-syntax/floats.md
Demali-876 Feb 14, 2025
94bc607
Update doc/md/meet-motoko.md
Demali-876 Feb 14, 2025
779701e
Update doc/md/fundamentals/basic-syntax/text.md
Demali-876 Feb 14, 2025
c51fce9
Update doc/md/fundamentals/basic-syntax/floats.md
Demali-876 Feb 14, 2025
f2ba6da
Update doc/md/fundamentals/basic-syntax/integers.md
Demali-876 Feb 14, 2025
f963f44
Update doc/md/meet-motoko.md
Demali-876 Feb 14, 2025
206b588
Update doc/md/fundamentals/basic-syntax/defining_an_actor.md
Demali-876 Feb 14, 2025
ec79380
Update doc/md/meet-motoko.md
Demali-876 Feb 14, 2025
1ca77bc
Update doc/md/fundamentals/basic-syntax/integers.md
Demali-876 Feb 14, 2025
a64550b
Update doc/md/fundamentals/basic-syntax/imports.md
Demali-876 Feb 14, 2025
16dce1c
Update doc/md/fundamentals/basic-syntax/literals.md
Demali-876 Feb 14, 2025
061e2dc
Update doc/md/fundamentals/basic-syntax/whitespace.md
Demali-876 Feb 14, 2025
f482fc5
Update doc/md/fundamentals/basic-syntax/printing_values.md
Demali-876 Feb 14, 2025
6b3302c
Update doc/md/fundamentals/basic-syntax/printing_values.md
Demali-876 Feb 14, 2025
7395f31
Update doc/md/fundamentals/basic-syntax/whitespace.md
Demali-876 Feb 14, 2025
0460822
Update doc/md/fundamentals/expressions/integers-numbers.md
Demali-876 Feb 14, 2025
78bdcc4
Update doc/md/fundamentals/basic-syntax/text.md
Demali-876 Feb 14, 2025
84ddef1
Update doc/md/fundamentals/basic-syntax/traps.md
Demali-876 Feb 14, 2025
b098633
nix fix + loops intro
Demali-876 Feb 15, 2025
42c7dc3
Merge branch 'docs-restructure' of https://github.com/Demali-876/moto…
Demali-876 Feb 15, 2025
c5755bf
updates
Demali-876 Feb 15, 2025
aa9691e
updates to expression declarations
Demali-876 Feb 17, 2025
829c299
Update doc/md/fundamentals/expressions/control-flow/block.md
Demali-876 Feb 19, 2025
92f1be3
Update doc/md/fundamentals/expressions/control-flow/variant.md
Demali-876 Feb 19, 2025
c5775a7
Update doc/md/fundamentals/expressions/control-flow/contol_flow_struc…
Demali-876 Feb 19, 2025
d28ae0c
Update doc/md/fundamentals/expressions/control-flow/contol_flow_struc…
Demali-876 Feb 19, 2025
8b8cfc7
Update doc/md/fundamentals/expressions/control-flow/contol_flow_struc…
Demali-876 Feb 19, 2025
b71be6f
Update doc/md/fundamentals/expressions/control-flow/for_loop.md
Demali-876 Feb 19, 2025
d5ea317
Update doc/md/fundamentals/expressions/control-flow/variant.md
Demali-876 Feb 19, 2025
6d9bf1d
Update doc/md/fundamentals/expressions/control-flow/variant.md
Demali-876 Feb 19, 2025
de37a03
Update doc/md/fundamentals/expressions/control-flow/variant.md
Demali-876 Feb 19, 2025
0dae779
Update doc/md/fundamentals/expressions/control-flow/variant.md
Demali-876 Feb 19, 2025
0c7b2e9
Update doc/md/fundamentals/expressions/control-flow/for_loop.md
Demali-876 Feb 19, 2025
cf36097
Update doc/md/fundamentals/expressions/control-flow/if-else.md
Demali-876 Feb 19, 2025
9d617a9
Update doc/md/fundamentals/expressions/control-flow/for_loop.md
Demali-876 Feb 19, 2025
0837d56
Update doc/md/fundamentals/expressions/control-flow/if-else.md
Demali-876 Feb 19, 2025
b637481
Update doc/md/fundamentals/expressions/control-flow/option.md
Demali-876 Feb 19, 2025
1c7c838
Update doc/md/fundamentals/expressions/control-flow/record.md
Demali-876 Feb 19, 2025
b944cf7
Update doc/md/fundamentals/expressions/control-flow/loop.md
Demali-876 Feb 19, 2025
e5830e6
Update doc/md/fundamentals/expressions/control-flow/option.md
Demali-876 Feb 19, 2025
b0c566d
Update doc/md/fundamentals/expressions/control-flow/record.md
Demali-876 Feb 19, 2025
edc0e55
Update doc/md/fundamentals/expressions/control-flow/record.md
Demali-876 Feb 19, 2025
b52b96e
Update doc/md/fundamentals/expressions/control-flow/record.md
Demali-876 Feb 19, 2025
96ab9e0
Update doc/md/fundamentals/expressions/control-flow/switch.md
Demali-876 Feb 19, 2025
5454823
Update doc/md/fundamentals/expressions/control-flow/tuple.md
Demali-876 Feb 19, 2025
989a3d2
Update doc/md/fundamentals/expressions/control-flow/tuple.md
Demali-876 Feb 19, 2025
253ba88
code comments and commit suggestions
Demali-876 Feb 19, 2025
42f3b8b
Delete doc/md/fundamentals/expressions/control-flow/control_flow.md
Demali-876 Feb 20, 2025
7252358
Rename contol_flow_structs.md to basic_control_flow.md
Demali-876 Feb 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions doc/md/fundamentals/basic-syntax/assertion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
sidebar_position: 14
---
# Assertions

An assertion checks a condition at runtime and traps if it fails.

```motoko
let n = 10;
assert n % 2 == 1; // Traps
```

```motoko
let n = 10;
assert n % 2 == 0; // Succeeds
```

Assertions help catch logic errors early, but should not be used for regular error handling.

<!---

Insert link to regular error handling doc when available

Insert link to base library reference for Assertions
-->
21 changes: 19 additions & 2 deletions doc/md/fundamentals/basic-syntax/characters.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
---
sidebar_position: 3
sidebar_position: 6
---

# Characters
# Characters

The `Char` type in Motoko represents a single unicode character (`'`) delimited.

```motoko
let letter: Char = 'A';
let symbol: Char = '☃';
```

Comparing characters

```motoko
'I' == 'i' //false
```

## Quick references

- [Base library Char](https://internetcomputer.org/docs/current/motoko/main/base/Char)
42 changes: 40 additions & 2 deletions doc/md/fundamentals/basic-syntax/comments.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
---
sidebar_position: 7
sidebar_position: 10
---

# Comments
# Comments

Motoko supports single-line, multi-line, and nested comments:

Use `//` for comments that extend to the end of a line.

```motoko
// This is a single-line comment
```

Use `/* ... */` for block comments spanning multiple lines.

```motoko
/* This is a
multi-line comment */
```

Multi-line comments can be nested within each other.

```motoko
/* Outer comment
/* Nested comment */
End of outer comment */
```

Use `///` for function or module documentation.

```motoko
/// Returns the sum of two integers.
func add(a: Int, b: Int): Int {
a + b
}
```

## Quick references

- [Comment style guide](https://internetcomputer.org/docs/current/motoko/main/reference/style/#comments)

- [Generating Motoko documentation](https://internetcomputer.org/docs/current/motoko/main/reference/generating-docs)
26 changes: 26 additions & 0 deletions doc/md/fundamentals/basic-syntax/defining_an_actor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
sidebar_position: 1
---
# Defining an actor

In Motoko, an actor is a unit of computation that encapsulates state and behavior. Unlike traditional functions or objects in other programming languages, actors operate independently and interact with each other through asynchronous messaging.

A Motoko program typically starts by defining an actor. The example below declares an actor named `Main` with a `hello` function that returns a `"Hello, world!"` message:

```motoko
actor Main {
public query func hello() : async Text {
"Hello, world!"
};
};

await Main.hello();
```

A Motoko actor always presents its interface as a suite of named functions with defined argument types and return types. When Motoko code is compiled, this interface is automatically generated in **[Candid](https://internetcomputer.org/docs/current/developer-docs/smart-contracts/candid/candid-concepts)**, an interface description language.

Since actors in Motoko communicate asynchronously, `await` ensures the result is retrieved once the function completes.

## Quick references

- [Actors](https://internetcomputer.org/docs/current/motoko/main/writing-motoko/actors-async)
15 changes: 13 additions & 2 deletions doc/md/fundamentals/basic-syntax/floats.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
---
sidebar_position: 2
sidebar_position: 5
---

# Floats
# Floats

Floating-point numbers in Motoko are represented using the `Float` type, which corresponds to a 64-bit double-precision floating-point number.

```motoko
let pi: Float = 3.14159;
let exp: Float = 2.71828;
```

## Quick references

- [Base library Float](https://internetcomputer.org/docs/current/motoko/main/base/Float)
10 changes: 8 additions & 2 deletions doc/md/fundamentals/basic-syntax/identifiers.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
---
sidebar_position: 9
sidebar_position: 11
---

# Identifiers
# Identifiers

Identifiers are names used for variables, functions, and other entities. They must start with a letter and can contain letters, digits, and underscores.

```motoko
let number = 10;
```
35 changes: 35 additions & 0 deletions doc/md/fundamentals/basic-syntax/imports.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
sidebar_position: 2
---

# Imports

Package imports should be at the top of the source file. Imports allow you to reuse code from external libraries or modules, making your code easier to maintain and manage. You can import from:

1. Standard modules provided by the base library:

```motoko
import Text "mo:base/Text";
```

2. Third-party packages installed via the Mops package manager:

```motoko
import Package "mo:packagename";
```

3. Files within your own project:

```motoko
import Utils "utils";
```

You can also import specific functions from a module:

```motoko
import { compare } "mo:base/Nat";
```

## Quick references

- [Modules and imports](https://internetcomputer.org/docs/current/motoko/main/writing-motoko/modules-and-imports/)
45 changes: 43 additions & 2 deletions doc/md/fundamentals/basic-syntax/integers.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
---
sidebar_position: 1
sidebar_position: 4
---

# Integers
# Integers

`Int` represents all integers, both positive and negative (e.g., -2, -1, 0, 1, 2).

For scenarios requiring fixed-size integers, Motoko offers bounded variants with specific bit-widths (`Int8`, `Int16`, `Int32`, `Int64`). These types can overflow if their limits are exceeded, resulting in a runtime error.

```motoko
let a: Int = -42;
let b: Int = 0;
let c: Int = 12345;
```

## Unbounded integers

By default, `Int` is unbounded, meaning it can grow as large (or as small) as needed without causing overflow:

```motoko
let bigNumber: Int = 999_999_999_999_999;
```

## Bounded integers

For scenarios requiring fixed-size integers, Motoko offers bounded integer types with specific bit-widths:

- `Int8` (8-bit signed integer)
- `Int16` (16-bit signed integer)
- `Int32` (32-bit signed integer)
- `Int64` (64-bit signed integer)

Bounded integers can overflow if their limits are exceeded, resulting in a runtime error:

```motoko
// let overflowInt: Int8 = 128; // Error: literal out of range Int8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a compile time error (the type-checker checks the literal).
You'd want to do something more dynamic like 127 + n where n > 0

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type checker raises an error if a value larger than 127 is explicitly assigned to Int8. But I agree a more dynamic approach would be useful for cases where a value starts within bounds but is later reassigned to a larger number.

public func triggerOverflow() : async Int8 {
var a :Int8 = 28;
var b : Int8 = 100;
a += b ;
a
};

```

## Quick references

- [Base library Int](https://internetcomputer.org/docs/current/motoko/main/base/Int)
- [Base library Int8](https://internetcomputer.org/docs/current/motoko/main/base/Int8)
- [Base library Int16](https://internetcomputer.org/docs/current/motoko/main/base/Int16)
- [Base library Int32](https://internetcomputer.org/docs/current/motoko/main/base/Int32)
- [Base library Int64](https://internetcomputer.org/docs/current/motoko/main/base/Int64)
17 changes: 15 additions & 2 deletions doc/md/fundamentals/basic-syntax/keywords.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
---
sidebar_position: 8
sidebar_position: 12
---

# Keywords
# Keywords

Motoko reserves certain words for its syntax and they cannot be used as identifiers. These include:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<!---
Notes:
Change this to a list and link to the docs that describe each keyword
-->

```motoko
actor and assert async async* await await*
break case catch class composite continue debug
debug_show do else false flexible finally for
from_candid func if ignore import in module
not null persistent object or label let
loop private public query return shared stable
switch system throw to_candid true transient try
type var while with
```
23 changes: 21 additions & 2 deletions doc/md/fundamentals/basic-syntax/literals.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
---
sidebar_position: 5
sidebar_position: 8
---

# Literals
# Literals

Literals are fixed values written directly in the code.

- Integer literals: `42`, `0x2A` (hexadecimal)
- Float literals: `3.14`, `2.5e3`
- Character literals: `'A'`, `'☃'`
- Text literals: `"Hello"`

You can use literals directly in expressions:

Numeric literal in expression

```motoko
100 + 50
```

## Quick references

- [Literals](https://internetcomputer.org/docs/current/motoko/main/reference/language-manual/#literals)
28 changes: 28 additions & 0 deletions doc/md/fundamentals/basic-syntax/printing_values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
sidebar_position: 3
---
# Printing values

Motoko uses `Debug.print` to output text to the terminal. It takes a `Text` value and returns `()`, meaning it has no meaningful return value.

```motoko
import Debug "mo:base/Debug";
Debug.print("Hello, world!");
```

For debugging, `debug_show` converts most Motoko values into `Text`. The function handles primitive types and simple collections well but may not work with nested, recursive, or complex custom types, which may require **transformation or unwrapping** before use.

```motoko
import Debug "mo:base/Debug";
Debug.print(debug_show(42)); // "42"
```

Since printing modifies output, it is considered an **impure function**, unlike pure functions that return values without side effects.

`debug_show(42)` is pure because it always returns `"42"` without affecting anything outside the function.

`Debug.print`("Hello, World!") is impure because it causes a side effect by printing to the console.

## Quick references

- [Base library Debug](https://internetcomputer.org/docs/current/motoko/main/base/Debug)
20 changes: 18 additions & 2 deletions doc/md/fundamentals/basic-syntax/text.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
---
sidebar_position: 4
sidebar_position: 7
---

# Text
# Text

Sequences of characters are handled using the `Text` type, which represents immutable strings of unicode characters (`"`) delimited.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<!---
Notes:
Insert link to base library reference
-->

```motoko
let greeting: Text = "Hello, world!";
```

Concatenating text

```motoko
"ICP" # "x" # "Motoko" //ICP x Motoko
```

## Quick references

- [Base library Text](https://internetcomputer.org/docs/current/motoko/main/base/Text)
20 changes: 20 additions & 0 deletions doc/md/fundamentals/basic-syntax/traps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
sidebar_position: 13
---
# Traps

A trap is a runtime error that causes execution to abort immediately. Common causes include division by zero, out-of-bounds array access and pattern match failure

If a trap occurs inside an actor message, only that message fails—other messages continue execution.

To trigger a trap manually, use `Debug.trap`:

```motoko
import Debug "mo:base/Debug";

Debug.trap("oops!");
```

## Quick references

-[Traps](https://internetcomputer.org/docs/current/motoko/main/writing-motoko/actors-async/#traps)
Loading