You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can implement simple arrays in Tact by relying on the map type.
4
+
5
+
To create an array, define a map with `Int` type as key. The key will hold the index in the array. Add another length variable to rememebr how many items are in the array.
6
+
7
+
The example contract records the last 5 timestamps of when the `timer` message was received. The timestamps are held in a cyclical array implemented as a map.
8
+
9
+
## Limit the number of items
10
+
11
+
Maps are designed to hold a limited number of items. Only use a map if you know the upper bound of items that it may hold. It's also a good idea to [write a test](https://github.com/tact-lang/tact-emulator) to add the maximum number of elements to the map and see how gas behaves under stress.
12
+
13
+
If the number of items is unbounded and can potentially grow to billions, you'll need to architect your contract differently. We will discuss unbounded arrays later on under the topic of contract sharding.
Maps are a dictionary type that can hold an arbitrary number of items, each under a different key.
4
+
5
+
The keys in maps can either be an `Int` type or an `Address` type.
6
+
7
+
You can check if a key is found in the map by calling the `get()` method. This will return `null` if the key is missing or the value if the key is found. Replace the value under a key by calling the `set()` method.
8
+
9
+
Integers in maps stored in state currently use the largest integer size (257-bit). Future versions of Tact will let you optimize the encoding size.
10
+
11
+
## Limit the number of items
12
+
13
+
Maps are designed to hold a limited number of items. Only use a map if you know the upper bound of items that it may hold. It's also a good idea to [write a test](https://github.com/tact-lang/tact-emulator) to add the maximum number of elements to the map and see how gas behaves under stress.
14
+
15
+
If the number of items is unbounded and can potentially grow to billions, you'll need to architect your contract differently. We will discuss unbounded maps later on under the topic of contract sharding.
0 commit comments