Value Types
4 minutes of reading
These types will always be passed by value, i.e. they are always copied when they are used as function arguments or assigned to another variable so that when you modify the copy you won’t change the value of the original variable.
int and uint
There are a number of types of int (signed integer) and uint (unsigned integer) versions at your disposal. int and uint are synonyms for int256 and uint256. For an integer type X, you can use type(X).min and type(X).max to access the minimum and maximum value representable by the type.
int i;
int8 i8;
int16 i16;
int24 i24;
// ... up to
int256 i256;
uint ui;
uint8 ui8;
uint16 ui16;
uint24 ui24;
// ... up to
uint256 = ui256;
You should use a smaller integer whenever possible because storage causes higher transaction fees.
You can typecast a smaller integer to a larger integer.
int8 a = -128;
int256 b = a;
Which is the same as
int8 a = -128;
int256 b = int256(a);
address
An address type in Solidity holds a 20-byte value, which is the size of an Ethereum address. An address payable type is the same as address, but with additional function transfer and send. You can send Ether to an address payable, but you can’t send Ether to a plain address.
The concept of payable and non-payable addresses only exists in the Solidity type system at compile-time. The difference between payable and non-payable addresses is gone in the compiled contract code.
address koalaAddress = 0x5520ADf9bB6179fD1065d57Ea3b0d286BD9F3858;
address payable payableKoalaAddress = payable(0x5520ADf9bB6179fD1065d57Ea3b0d286BD9F3858);
address payable can be implicitly or explicitly cast to address, whereas conversions from address to address payable must be explicit via payable([address]). In the example below, msg.sender is a global variable which stores the address of the account which initiates the current transaction. If you are deploying this contract then msg.sender will be equal to your account address. However, later if you or someone else calls the public functions within your deployed contract, then msg.sender will be yours or someone else’s account address respectively.
address payable addr1 = payable(msg.sender);
address addr2 = addr1;
address addr3 = address(addr1);
Explicit conversions to and from address are allowed for uint160, integer literals, bytes20 and contract types.
address addr1 = address(0x5520ADf9bB6179fD1065d57Ea3b0d286BD9F3858);
address addr2 = address(131);
Enum
This can be used to create custom types with a finite set of values.
enum Grade {
FAIL,
PASS,
CREDIT,
DISTINCTION,
HIGHER_DISTINCTION
}
Grade myGrade = Grade.HIGHER_DISTINCTION;