Skip to content

Commit 89bfc54

Browse files
committed
spec: cover indexing expressions
1 parent a61c15f commit 89bfc54

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

docs/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
* [Call expressions](spec/expr_call.md)
4444
* [Tuple expressions](spec/expr_tuple.md)
4545
* [List expressions](spec/expr_list.md)
46+
* [Index expressions](spec/expr_index.md)
4647
* [Attribute expressions](spec/expr_attribute.md)
4748
* [Literal expressions](spec/expr-literal.md)
4849
* [Arithmetic Operators](spec/arithmetic_operators.md)

docs/src/spec/expr_index.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Index expressions
2+
3+
> **<sup>Syntax</sup>**\
4+
> _IndexExpression_ :\
5+
> &nbsp;&nbsp; [_Expression_] `[` [_Expression_] `]`
6+
7+
[Array] and [Map] types can be indexed by by writing a square-bracket-enclosed expression after them. For arrays, the type of the index key has to be `u256` whereas for [Map] types it has to be equal to the key type of the map.
8+
9+
10+
Example:
11+
12+
```python
13+
contract Foo:
14+
15+
balances: Map<address, u256>
16+
17+
pub fn baz(values: u256[10]):
18+
# Assign value at slot 5
19+
values[5] = 1000
20+
# Read value at slot 5
21+
let val1: u256 = values[5]
22+
23+
# Assign value for address zero
24+
self.balances[address(0)] = 10000
25+
26+
# Read balance of address zero
27+
let bal: u256 = self.balances[address(0)]
28+
```
29+
30+
[_Expression_]: expressions.md
31+
[Array]: array_types.md
32+
[Map]: hashmap_types.md

docs/src/spec/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* [Call expressions](expr_call.md)
3838
* [Tuple expressions](expr_tuple.md)
3939
* [List expressions](expr_list.md)
40+
* [Index expressions](expr_index.md)
4041
* [Attribute expressions](expr_attribute.md)
4142
* [Literal expressions](expr-literal.md)
4243
* [Arithmetic Operators](arithmetic_operators.md)

0 commit comments

Comments
 (0)