Skip to content

Commit

Permalink
add introduction to unit type
Browse files Browse the repository at this point in the history
  • Loading branch information
skylee03 committed Apr 3, 2024
1 parent 9956335 commit 6a35a7d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 12 additions & 2 deletions course2/course_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,15 @@ MoonBit has a static type system: the compiler performs type checking before run

---

# Unit

- A type which has only one value.
- `(): Unit`
- It enables the treatment of statements as expressions.
- In MoonBit, all statements evaluate to `()`.

---

# Other Data Types

- MoonBit has a rich type system.
Expand Down Expand Up @@ -530,8 +539,9 @@ $$\begin{align}
\mapsto & \texttt{10}
\end{align}$$

A conditional expression must have two branches.
- Otherwise, if the condition is false, what should be the value of the expression?
If the true branch ends with a statement, the false branch can be omitted.
- Implicitly, there is a hidden false branch returning `()`.
- Therefore, the two branches still have the same type, i.e., `Unit`.

---

Expand Down
8 changes: 6 additions & 2 deletions course2/lecture_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,13 @@ We can access the data by using zero-based indexing.
- `(2023, 10, 24).0 == 2023`
- `(2023, 10, 24).1 == 10`

#### Unit

In MoonBit, the `Unit` type represents a singular value denoted as `()`. Though seemingly useless, it holds significant implications as it enables the treatment of statements as expressions: in MoonBit, all statements evaluate to `()`.

#### Other Data Types

MoonBit has a rich type system, which includes many other types that we have not yet discussed, such as function types, unit types, and list types. These types will be explored in detail in future modules.
MoonBit has a rich type system, which includes many other types that we have not yet discussed, such as function types and list types. These types will be explored in detail in future modules.

### Expression Evaluation

Expand Down Expand Up @@ -363,7 +367,7 @@ $$\begin{align}
\mapsto & \texttt{10}
\end{align}$$

A conditional expression must have two branches. Otherwise, if the condition is false, what should be the value of the expression?
If the true branch ends with a statement, the false branch can be omitted. Implicitly, there is a hidden false branch returning `()`, and therefore the two branches still have the same type, i.e., `Unit`.

## Summary

Expand Down

0 comments on commit 6a35a7d

Please sign in to comment.