forked from mun-lang/book
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: added some basic language constructs to the docs
* refactor: integrated highlight.js with mun support * docs: added some basic language constructs to the docs Co-Authored-By: Wodann <[email protected]>
- Loading branch information
1 parent
4fa385b
commit 876f6a0
Showing
6 changed files
with
155 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Basic Concepts | ||
|
||
This section describes the basic concepts of the Mun programming language. | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Values and types | ||
|
||
Mun is a statically typed language, which helps to detect type-related errors at | ||
compile-time. A type error is an invalid operation on a given type, such as an | ||
integer divided by a string, trying to access a field that doesn't exist, or | ||
calling a function with the wrong number of arguments. | ||
|
||
Some languages require a programmer to explicitly annotate syntactic constructs | ||
with type information: | ||
|
||
```c++ | ||
int foo = 3 + 4; | ||
``` | ||
|
||
However, often variable types can be inferred by their usage. Mun uses type | ||
inferencing to determine variable types at compile time. However, you are still | ||
forced to explicitly annotate variables in a few locations to ensure a contract | ||
between interdependent code. | ||
|
||
```mun | ||
fn bar(a:int):int { | ||
let foo = 3 + a | ||
foo | ||
} | ||
``` | ||
|
||
Here, the parameter `a` and the return type must be annotated because it solidifies | ||
the signature of the function. The type of `foo` can be inferred through its | ||
usage. | ||
|
||
## Basic types | ||
|
||
Mun knows two basic numeric types: `float` and `int`. A `float` is a | ||
double-precision IEEE 64-bit floating point number and an `int` represents a | ||
64-bit integral number. | ||
|
||
In Mun an `int` can be explicitly cast to `float`, but the reverse is not true. | ||
Assigning a `float` to an `int` might cause loss of precision and can therefore | ||
not be cast implicitly. | ||
|
||
A `bool` has two possible values: `true` and `false`. | ||
|
||
## Implicit & explicit returns | ||
|
||
A block is a grouping of statements and expressions surrounded by curly braces. | ||
Function bodies are an example of blocks. In Mun, blocks evaluate to the last | ||
expression in them. Blocks can therefore also be used on the right hand side of a | ||
`let` statement. | ||
|
||
```mun | ||
fn foo():int { | ||
let bar = { | ||
let b = 3; | ||
b+3 | ||
} | ||
// `bar` has a value 6 | ||
bar+3 | ||
} | ||
``` | ||
|
||
Besides implicit returns, explicit returns can also be used with `return` | ||
expressions. However, explicit `return` statements always return from the | ||
function, not from the block: | ||
|
||
```mun | ||
fn foo():int { | ||
let bar = { | ||
let b = 3; | ||
return b+3 | ||
} | ||
// This code will never be executed | ||
return bar+3; | ||
} | ||
``` | ||
|
||
## Shadowing | ||
|
||
> WIP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Control flow | ||
|
||
Executing or repeating a block of code only under specific conditions are common | ||
constructs that allow developers to control the flow of execution. Mun provides | ||
`if...else` expressions and loops. | ||
|
||
## `if` expressions | ||
|
||
An `if` expression allows you to branch your code depending on conditions. | ||
|
||
```mun | ||
fn main() { | ||
let number = 3; | ||
if number < 5 { | ||
number = 4; | ||
} else { | ||
number = 6; | ||
} | ||
} | ||
``` | ||
|
||
All `if` expressions start with the keyword `if`, followed by a condition. | ||
As opposed to many C-like languages, Mun omits parentheses around the | ||
condition. Only when the condition is true - in the example, whether the `number` | ||
variable is less than 5 - the consecutive code block (or *arm*) is executed. | ||
|
||
Optionally, an `else` expression can be added that will be executed when the | ||
condition evaluates to false. You can also have multiple conditions by combining | ||
`if` and `else` in an `else if` expression. For example: | ||
|
||
```mun | ||
fn main() { | ||
let number = 6; | ||
if number > 10 { | ||
// The number if larger than 10 | ||
} else if number > 8 { | ||
// The number is larger than 8 but smaller or equal to 10 | ||
} else if number > 2 { | ||
// The number is larger than 2 but smaller or equal to 8 | ||
} else { | ||
// The number is smaller than- or equal to 2. | ||
} | ||
} | ||
``` | ||
|
||
|
||
### Using `if` in a `let` statement | ||
|
||
The `if` expression can be used on the right side of a `let` statement | ||
just like a block: | ||
|
||
```mun | ||
fn main() { | ||
let condition = true; | ||
let number = if condition { | ||
5 | ||
} else { | ||
6 | ||
}; | ||
``` | ||
|
||
Depending on the condition, the `number` variable will be bound to the value | ||
of the `if` block or the `else` block. This means that both the `if` and `else` | ||
arms need to evaluate to the same type. | ||
If the types are mismatched the compiler will report an error. |