I was reading a book on solidity, the array was defined as
int age= [int(10),20,30,40,50]
When data type int was declared at the start then why it is declared again in the array with 10?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.Sign up to join this community
Solidity determines the type of array literal (
[int(10),20,30,40,50]) as the smallest type all the elements could be implicitly converted into. Here element types are:
uint8, so the smallest type all elements may could be converted into is
int. So the type of array literal is
If you omit
int(...), then types of all the elements would be
uint8, thus the whole literal would have type
uint8 which is incompatible with the type of the variable this value is about to be assigned to.
See documentation for details.