I have a struct that has an array of addresses. When the address array is empty and I try to get index 0 I would expect it to return address(0), but instead it reverts. Any way around this?
struct KeyedMapAddressToBool {
mapping(address => bool) data;
mapping(address => uint) indices;
address[] addresses;
}
function set(KeyedMapAddressToBool storage map, address key, bool value) public {
map.data[key] = value;
// THIS REVERTS
address a = map.addresses[0];
}
EDIT:
I updated my struct to contain an exists mapping, but am wondering if there is another way. This works:
struct KeyedMapAddressToBool {
mapping(address => bool) data;
mapping(address => uint) indices;
address[] addresses;
mapping(address => bool) exists;
}
function set(KeyedMapAddressToBool storage map, address key, bool value) public {
map.data[key] = value;
if (map.exists[key] == false) {
map.indices[key] = map.addresses.length;
map.addresses.push(key);
map.exists[key] = true;
}
}