0

I have come across statements like uint256(0) and address(0). What is the meaning of that?

For example this statement in solidity docs uint256(0) - uint256(1) == 2**256 - 1

2

It's just casting a value explicity into a specific type. There are a few everyday uses for it.

As a guard:

require(msg.sender != address(0), "You forgot to populate the address field.");

It's also useful for constants in a contract because, by default, they will cast to the smallest possible precision.

This example is a little contrived because Solidity has improved since this: Unexpected implicit casting in Solidity's exponential operator.

pragma solidity 0.5.8;

contract Cast {

    function weirdResult() public pure returns(uint) {
        uint8 y = 3;
        uint8 x = 10;
        return x**y;
    }
}

10 to the power of 3 is 232? Yikes!

You will see constants wrapped in a type to avoid all doubt about how they should be cast. It's a good safety habit.

return uint(10)**uint(3); // less possibility of misinterpretation

instead of

return 10**3;

As a general style thing, I like to avoid using any hard-coded values deep in the contracts and declare them up front, instead.

address constant NULL_ADDRESS = address(0);
bytes32 constant NULL_KEY = bytes32(0);

You could even

uint constant NO_MONEY = uint(0);

...

require(msg.value != NO_MONEY, "You have to send some ether.");

Hope it helps.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.