Skip to content

Commit

Permalink
Merge pull request #309 from AleoHQ/craigjson/add-array-docs
Browse files Browse the repository at this point in the history
Add Leo Array Syntax Documentation
  • Loading branch information
craigjson authored Nov 6, 2023
2 parents 95936c7 + 974d773 commit 37b8ef4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
53 changes: 53 additions & 0 deletions documentation/leo/03_language.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,59 @@ record token {
}
```

### Array

Leo supports static arrays. Arrays are declared as `[type; length]`. Arrays cannot be empty nor modified.

Arrays only support constant accesses (the index must be a constant value). The accessor value must be a constant integer.

Arrays can contain primitive data types, structs, or arrays. Structs and records can also contain arrays.

Arrays can be iterated over using a for loop.

```leo
// Initalize a boolean array of length 4
let arr: [bool; 4] = [true, false, true, false];
// Nested Array
let nested: [[bool; 2]; 2] = [[true, false], [true, false]];
// Array of Structs
struct bar {
data: u8,
}
let arr_of_structs: [bar; 2] = [bar { data: 1u8 }, bar { data: 2u8 }];
// Access the field of a struct within an array
transition foo(a: [bar; 8]) -> u8 {
return a[0u8].data;
}
// Struct that contains an array
struct bat {
data: [u8; 8],
}
// Record that contains an array
record floo {
owner: address,
data: [u8; 8],
}
// Declare a mapping that contains an array value
mapping data: address => [bool; 8];
// Iterate over an array using a for loop and sum the values within
transition sum_with_loop(a: [u64; 4]) -> u64 {
let sum: u64 = 0u64;
for i: u8 in 0u8..4u8 {
sum += a[i];
}
return sum;
}
```

### Transition Function

Transition functions in Leo are declared as `transition {name}() {}`.
Expand Down
19 changes: 12 additions & 7 deletions documentation/leo/09_cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,27 @@ struct message {
}
```

## 6. Transitions
## 6. Arrays
```leo
let arr: [bool; 2] = [true, false];
```

## 7. Transitions
```leo
transition mint_public(
public receiver: address,
public amount: u64,
) -> token { /* Your code here */ }
```

## 7. Functions
## 8. Functions
```leo
function compute(a: u64, b: u64) -> u64 {
return a + b;
}
```

## 8. Inline Functions
## 9. Inline Functions
```leo
inline foo(
a: field,
Expand All @@ -84,7 +89,7 @@ inlines can only call inlines.
Direct/indirect recursive calls are not allowed


## 9. For Loops
## 10. For Loops
```leo
let count: u32 = 0u32;
Expand All @@ -93,7 +98,7 @@ for i: u32 in 0u32..5u32 {
}
```

## 10. Mappings
## 11. Mappings
```leo
mapping balances: address => u64;
Expand All @@ -104,7 +109,7 @@ let set_bal: () = Mapping::set(balances, receiver, 100u64);
let remove_bal: () = Mapping::remove(balances, receiver);
```

## 11. Commands
## 12. Commands
```leo
transition matches(height: u32) {
return then finalize(height);
Expand All @@ -129,7 +134,7 @@ assert_neq(a, b); // assert a and b are not equal
```


## 12. Operators
## 13. Operators
```leo
let sum: u64 = a + b; // arithmetic addition
let diff: u64 = a - b; // arithmetic subtraction
Expand Down

0 comments on commit 37b8ef4

Please sign in to comment.