Skip to content

Commit

Permalink
Explain bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
kyouko-taiga committed Jul 4, 2022
1 parent e47573a commit e278810
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The code of the compiler is open source and [hosted on GitHub](https://github.co
## Enough, show me some code!

Okay, okay.
Here's a simple program.
Here's a simple program:

```val
subscript longer_of(_ a: inout String, _ b: inout String): String {
Expand Down
60 changes: 60 additions & 0 deletions language-tour.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,66 @@ To run this program:
- Run the command `valc --modules Sources/Hello Sources/Greet -o hello`
- Run the command `./hello`

## Bindings

A binding is a name that denotes an object or a projection (more on that later).
Bindings can be mutable or not.
The value of a mutable can be modified whereas that of an immutable binding cannot.

Immutable bindings are declared with `let` and can be initialized with the `=` operator:
It is not possible to modify their value after their initialization.

```val
public fun main() {
let gravity = 9.81
gravity = 11.2 // error: cannot assign, `gravity` is a `let` binding
}
```

Mutable bindings are typically declared with `var`.

```val
public fun main() {
var length = 1
length = 2
print(length) // 2
}
```

Bindings declared with `inout` are also mutable but operate differently.
They *project* the value (or part thereof) of an object mutably.

```val
public fun main() {
var point = (x: 0.0, y: 1.0)
inout x = &point.x
x = 3.14
print(point) // (x: 3.14, y: 1.0)
}
```

Val is statically typed: bindings of a given type cannot be assigned a value of a different one.
For instance, it is impossible to assign a floating point number to an integer binding:

```val
public fun main() {
var length = 1
length = 2.3 // error: expected type `Int`, found `Double`
}
```

The type of a binding is determined at declaration.
If an initializing expression is present, such as in all previous examples, the binding is given the type of that expression.
Alternatively, we may state the type of a binding explicitly by the means of an annotation:

```val
public fun main() {
var weight: Double = 1
weight = 2.3
print(weight) // 2.3
}
```

* * *

[Home](./)

0 comments on commit e278810

Please sign in to comment.