Skip to content

Commit

Permalink
spec: cover indexing expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
cburgdorf committed Aug 23, 2021
1 parent 890484c commit eb09d5d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* [Call expressions](spec/expr_call.md)
* [Tuple expressions](spec/expr_tuple.md)
* [List expressions](spec/expr_list.md)
* [Index expressions](spec/expr_index.md)
* [Attribute expressions](spec/expr_attribute.md)
* [Literal expressions](spec/expr-literal.md)
* [Arithmetic Operators](spec/arithmetic_operators.md)
Expand Down
32 changes: 32 additions & 0 deletions docs/src/spec/expr_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Index expressions

> **<sup>Syntax</sup>**\
> _IndexExpression_ :\
> &nbsp;&nbsp; [_Expression_] `[` [_Expression_] `]`
[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.


Example:

```python
contract Foo:

balances: Map<address, u256>

pub fn baz(values: u256[10]):
# Assign value at slot 5
values[5] = 1000
# Read value at slot 5
let val1: u256 = values[5]

# Assign value for address zero
self.balances[address(0)] = 10000

# Read balance of address zero
let bal: u256 = self.balances[address(0)]
```

[_Expression_]: expressions.md
[Array]: array_types.md
[Map]: hashmap_types.md
1 change: 1 addition & 0 deletions docs/src/spec/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* [Call expressions](expr_call.md)
* [Tuple expressions](expr_tuple.md)
* [List expressions](expr_list.md)
* [Index expressions](expr_index.md)
* [Attribute expressions](expr_attribute.md)
* [Literal expressions](expr-literal.md)
* [Arithmetic Operators](arithmetic_operators.md)
Expand Down

0 comments on commit eb09d5d

Please sign in to comment.