I am trying to find a way to increase value of map, here is my code
import "@stdlib/deploy";
contract Holders with Deployable {
holders: map<Address, Int>;
owner: Address;
init() {
self.owner = sender();
}
receive("start") {
let amount: Int? = self.holders.get(sender());
self.holders.set(sender(), amount + 100);
}
get fun balance(key: Address): Int? {
return self.holders.get(key);
}
}
I must type as 'Int?' of amount variable's state, if I not type like it gives error, but if I type as 'Int?' it gives error for addition at 'amount + 100'.
Is there a way to increase value of map?
If you sure that key is exists in map you can use !!
operator to cast Int?
to Int
let amount: Int? = self.holders.get(sender());
if (amount != null) {
self.holders.set(sender(), amount!! + 100);
}
It should works fine