Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 49 additions & 31 deletions documentation/language/03_data_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,50 +130,48 @@ program test.aleo {

### Arrays

Leo supports static arrays. Array types are declared as `[type; length]` and can be nested.

Arrays only support constant accesses. The accessor expression must be a constant expression.

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 supports static arrays. Array types are declared as `[type; length]`. Elements can only be primitive data types, structs, or nested arrays.
```leo
// Initalize a boolean array of length 4
let arr: [bool; 4] = [true, false, true, false];

// Empty array
let empty: [u32; 0] = [];

// Nested array
let nested: [[bool; 2]; 2] = [[true, false], [true, false]];
```

// Empty array
let empty: [u32; 0] = [];
Structs and records can also contain arrays as fields.
```leo
record Foo {
owner: address,
data: [u8; 8],
}

struct Bar {
data: u8,
data: [u8; 8],
}

// Array of structs
let arr_of_structs: [Bar; 2] = [Bar { data: 1u8 }, Bar { data: 2u8 }];
```


Arrays only support constant accesses. The accessor expression must be a constant expression (known at compile-time).
```leo
// 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],
}

Arrays can be stored as a mapping input/output, and iterated over using a loop.
```leo
// Declare a mapping that contains array values
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;
Expand All @@ -186,22 +184,42 @@ transition sum_with_loop(a: [u64; 4]) -> u64 {

### Tuples

Leo supports tuples. Tuple types are declared as `(type1, type2, ...)` and can be nested. Tuples cannot be empty.
Leo supports tuples. Tuple types are declared as `(type1, type2, ...)` and cannot be empty.

Tuples only support constant access with a dot `.` and a constant integer.
Tuples can contain primitive data types, structs, arrays, or nested tuples. Structs and records can also contain tuples.

Tuples can contain primitive data types, structs, or arrays. Structs and records can also contain tuples.
```leo
// Initalize a boolean array of length 4
let tup: (u8,u8,bool) = (1u8,1u8,true);

// Nested array
let nested: [[bool; 2]; 2] = [[true, false], [true, false]];
```

Structs and records can also contain tuples as fields.
```leo
program test.aleo {
transition baz(foo: u8, bar: u8) -> u8 {
let a: (u8, u8) = (foo, bar);
let result: u8 = a.0 + a.1;
return result;
}
record Foo {
owner: address,
data: (u8,u8),
}

struct Bar {
data: (u8,u8),
}

// Tuple of structs
let tup_of_structs: [Bar; 2] = [Bar { data: (1u8,1u8) }, Bar { data: (2u8,2u8) }];
```

Tuples only support constant access with a dot `.` and a constant integer.

```leo
transition baz(foo: u8, bar: u8) -> u8 {
let a: (u8, u8) = (foo, bar);
let result: u8 = a.0 + a.1;
return result;
}
```

### Structs

Expand Down
4 changes: 2 additions & 2 deletions documentation/language/08_cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,8 @@ let rand: u32 = ChaCha::rand_u32(); // generate a random value `ChaCha::rand_<ty
// Hash Functions (BHP, Pedersen, Poseidon, Keccak, SHA3)
let hash: field = BHP256::hash_to_field(1u32); // hash any type to any type
let hash_raw: address = Poseidon2::hash_to_address_raw(1u8); // hash any raw type to any type
let hash_native: [bool; 256] = Keccak256::hash_native(0field); // hash any type to an array of bits (only available for Keccak and SHA3)
let hash_native_raw: [bool; 256] = Keccak256::hash_native_raw(0field); // hash any raw type to an array of bits (only available for Keccak and SHA3)
let hash_native: [bool; 256] = Keccak256::hash_to_bits(0field); // hash any type to an array of bits (only available for Keccak and SHA3)
let hash_native_raw: [bool; 256] = Keccak256::hash_to_bits_raw(0field); // hash any raw type to an array of bits (only available for Keccak and SHA3)

// Commitment Algorithms (BHP, Pedersen)
let commit: group = Pedersen64::commit_to_group(1u64, 1scalar); // commit any type to a field, group, or address, using a scalar as blinding factor (salt)
Expand Down
Loading