diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 69a5b2c..d7c7e88 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -3,7 +3,7 @@ [Introduction](ch00-00-introduction.md) [Case studies](ch00-01-case-studio-abbey-games.md) -## Types - -- [Types](ch01-00-types.md) +- [Basic concepts](ch01-00-basic-concepts.md) + - [Values and types](ch01-01-values-and-types.md) + - [Control flow](ch01-02-control-flow.md) diff --git a/src/ch01-00-basic-concepts.md b/src/ch01-00-basic-concepts.md new file mode 100644 index 0000000..5cc0d2c --- /dev/null +++ b/src/ch01-00-basic-concepts.md @@ -0,0 +1,5 @@ +# Basic Concepts + +This section describes the basic concepts of the Mun programming language. + + diff --git a/src/ch01-00-types.md b/src/ch01-00-types.md deleted file mode 100644 index dfd1d65..0000000 --- a/src/ch01-00-types.md +++ /dev/null @@ -1,37 +0,0 @@ -# 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, as illustrated -above. Mun uses type inferencing to determine variable types at compile time. -You are still forced to explicitly annotate variables in a few locations to ensure -a contract between interdependent code. - -```mun -fn bar(a:int) { - let foo = 3 + a -} -``` - -Here, the parameter `a` must be annotated because it solidifies the signature of -the function. The type of `foo` can be inferred through its usage. - -## Numeric 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. diff --git a/src/ch01-01-values-and-types.md b/src/ch01-01-values-and-types.md new file mode 100644 index 0000000..b1e95fa --- /dev/null +++ b/src/ch01-01-values-and-types.md @@ -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 diff --git a/src/ch01-02-control-flow.md b/src/ch01-02-control-flow.md new file mode 100644 index 0000000..b2f2e9b --- /dev/null +++ b/src/ch01-02-control-flow.md @@ -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. diff --git a/vendor/highlight.js b/vendor/highlight.js index 35b0abb..55f5d27 160000 --- a/vendor/highlight.js +++ b/vendor/highlight.js @@ -1 +1 @@ -Subproject commit 35b0abb4d7c5e4c78229e3df0dbb561dfbdb8942 +Subproject commit 55f5d2726d64ef31441f2d0fa3d910237bea4d22