Solidity – Checking Zero Value of Structure

solidity

I have the following statements:

Voter sender = voters[msg.sender];
if (!sender || sender.voted) throw;

I'm new to solidity, so I might not be aware of how (or if) coercion works.

As I found out in this question,

[…] every possible key exists and is mapped to a value whose byte-representation is all zeros […]

Knowing that, shouldn't the !sender statement above return true?

Best Answer

If Voter is a struct, the code !voter[sender] will probably result in an error. In your case, you'll want to check the voted or another field in the Voter struct.

For now, checking guards via voters[msg.sender].voted == 0 is probably the most reliable approach.

As a side note, strings can be a bit tricker: some_string == 0 will not work. In this case I use:

function(string value) { 
   if (bytes(value).length == 0)
     ...don't do it...
}
Related Topic