Solidity documentation says it is possible to use anything as mapping value, which I assume includes a contract:
_ValueType
can actually be any type, including mappings.
but I couldn't find an example or documentation on how to work with it.
If I have the following mapping, how can I know if the contract in the mapping exists, or have suicided?
pragma solidity ^0.4.0;
contract MyContract {
// ...
}
contract MappingExample {
mapping(uint32 => MyContract) inner_contracts;
function do_something(uint32 key) {
MyContract c;
if(/* inner_contracts[key] exists??? */) {
c = inner_contracts[key];
} else {
c = new MyContract;
inner_contracts[key] = c;
}
// do stuff with c
}
}
Best Answer
Here is an example.
If you play with this code, you'll notice that calling initialize before makeNew doesn't work. The reason is that mapping has default value filled with zeros. In case of contract it's not a good idea.
Let's use another mapping for flags!
If value is false(which is by default) then you should initialize your contract.