Why does this fail to compile on Solidity?

contract TestContract {
    function testFunc() {
        int256 constant testVar = 1;

It compiles if I remove the constant keyword.


In Solidity only state variables can be declared as constant.

State variables can be declared as constant. In this case, they have to be assigned from an expression which is a constant at compile time

Ref: http://solidity.readthedocs.io/en/latest/contracts.html#constant-state-variables

So just move the variable in the contract scope instead of function scope and it will work.

| 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.