Solidity

Solidity Type Deduction

Solidity Type Deduction Main Tips

  • For reasons concerning convenience, explicitly specifying the type of a variable is not necessary.
  • If no particular type is referred to, the compiler sets it automatically.

Solidity Type Deduction

For reasons concerning convenience, explicitly specifying the type of a variable is not necessary.

If no particular type is referred to, the compiler sets it automatically. This is done based on the type of the first expression that has been assigned to the variable, like in the example below:

Example

uint24 y = 0x123;
var x = y;

 

Try on Remix Try live on Hosting

In this case, the type of variable x is going to be uint24. However, var cannot be used for declaring return or function parameters.

Warning: The type only gets deduced from the very first assignment, therefore loop in the code example here is infinite, since variable i will has the type uint8 and the greatest value of this type is not bigger than 2000 : for (var i = 0; i < 2000; i++) { . . . } .

Read previous post:
Solidity Type Conversion

Solidity Type Conversion Main Tips Normally, when an operator has been assigned to variables of different types, implicitly, the compiler will...

Close