Tact does not support traditional for loops, but its loop statements are equivalent and can easily implement the same things. Also note that Tact does not support break and continue statements in loops like some languages.
The repeat loop statement input number must fit within an int32, otherwise an exception will be thrown.
The condition of the while and until loop statements can be any boolean expression.
Smart contracts consume gas for execution. The amount of gas is proportional to the number of iterations. The last example iterates too many times and reverts due to an out of gas exception.
All Examplesimport "@stdlib/deploy";
struct TokenInfo {
ticker: String;
decimals: Int;
}
// messages can contain maps
message Replace {
items: map;
}
contract Maps with Deployable {
// maps with Int as key
mi1: map;
mi2: map;
mi3: map;
mi4: map;
// maps with Address as key
ma1: map;
ma2: map;
ma3: map;
ma4: map;
init(arg: map) {
// no need to initialize maps if they're empty
self.mi2 = arg;
}
receive("set keys") {
// keys are Int
self.mi1.set(17, TokenInfo{ticker: "SHIB", decimals: 9});
self.mi2.set(0x9377433ff21832, true);
self.mi3.set(pow(2,240), pow(2,230));
self.mi4.set(-900, address("EQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqB2N"));
// keys are Address
self.ma1.set(address("EQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqB2N"), TokenInfo{ticker: "DOGE", decimals: 18});
self.ma2.set(address("UQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqEBI"), true);
self.ma3.set(address("EQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqB2N"), ton("1.23"));
self.ma4.set(address("UQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqEBI"), myAddress());
}
receive("delete keys") {
// keys are Int
self.mi1.set(17, null);
self.mi2.set(0x9377433ff21832, null);
self.mi3.set(pow(2,240), null);
self.mi4.set(-900, null);
// keys are Address
self.ma1.set(address("EQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqB2N"), null);
self.ma2.set(address("UQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqEBI"), null);
self.ma3.set(address("EQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqB2N"), null);
self.ma4.set(address("UQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqEBI"), null);
}
receive("clear") {
self.mi1 = emptyMap();
self.mi2 = emptyMap();
self.mi3 = emptyMap();
self.mi4 = emptyMap();
self.ma1 = emptyMap();
self.ma2 = emptyMap();
self.ma3 = emptyMap();
self.ma4 = emptyMap();
}
receive(msg: Replace) {
// replace all items in the map with those coming in the message
self.mi4 = msg.items;
}
// if the key is not found, the get() method returns null
get fun oneItem(key: Int): Address? {
return self.mi4.get(key);
}
get fun itemCheck(): String {
if (self.mi1.get(17) == null) {
return "not found";
}
let item: TokenInfo = self.mi1.get(17)!!; // !! tells the compiler this can't be null
return item.ticker;
}
// you can return maps from getters
get fun allItems(): map {
return self.ma1;
}
}