Skip to content

Commit

Permalink
Merge pull request #140 from moonbitlang/clean-var-shorthand
Browse files Browse the repository at this point in the history
clean `:=` `var` in readme
  • Loading branch information
bzy-debug authored Jan 12, 2024
2 parents 6ca53c4 + 4603869 commit 638c528
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ String interpolation is a powerful feature in MoonBit that enables you to substi

```swift live
fn init {
x := 42
let x = 42
print("The answer is \(x)")
}
```
Expand Down Expand Up @@ -341,7 +341,7 @@ fn init {

## Variable Binding

A variable can be declared as mutable or immutable using the keywords `var` or `let`, respectively. A mutable variable can be reassigned to a new value, while an immutable one cannot.
A variable can be declared as mutable or immutable using `let mut` or `let`, respectively. A mutable variable can be reassigned to a new value, while an immutable one cannot.

```go live
let zero = 0
Expand All @@ -353,17 +353,6 @@ fn init {
}
```

There is a short-hand syntax sugar for local immutable bindings, e.g, using `:=`.

```go
fn init {
a := 3
b := "hello"
print(a)
print(b)
}
```

## Data Types

There are two ways to create new data types: `struct` and `enum`.
Expand Down Expand Up @@ -474,7 +463,7 @@ fn init {

## Pattern Matching

We have shown a use case of pattern matching for enums, but pattern matching is not restricted to enums. For example, we can also match expressions against Boolean values, numbers, characters, strings, tuples, arrays, and struct literals. Since there is only one case for those types other than enums, we can pattern match them using `let`/`var` binding instead of `match` expressions. Note that the scope of bound variables in `match` is limited to the case where the variable is introduced, while `let`/`var` binding will introduce every variable to the current scope. Furthermore, we can use underscores `_` as wildcards for the values we don't care about, use `..` to ignore remaining fields of struct or elements of array.
We have shown a use case of pattern matching for enums, but pattern matching is not restricted to enums. For example, we can also match expressions against Boolean values, numbers, characters, strings, tuples, arrays, and struct literals. Since there is only one case for those types other than enums, we can pattern match them using `let` binding instead of `match` expressions. Note that the scope of bound variables in `match` is limited to the case where the variable is introduced, while `let` binding will introduce every variable to the current scope. Furthermore, we can use underscores `_` as wildcards for the values we don't care about, use `..` to ignore remaining fields of struct or elements of array.

```go
let id = match u {
Expand Down

0 comments on commit 638c528

Please sign in to comment.