2

What is the right way to instantiate a constant variable in Solidity? The following code give me errors when I compile:

pragma solidity ^0.4.4;

contract LearnSol {

    function LearnSol() {

    }

    function basicSyntax() returns(uint) {

        uint constant a = 8;  
        return 1;
    }
}

The error:

Compiling ./contracts/LearnSol.sol...

/usr/local/lib/node_modules/truffle/node_modules/solc/wrapper.js:48
        throw e;
        ^
abort(5) at Error
    at jsStackTrace (/usr/local/lib/node_modules/truffle/node_modules/solc/soljson.js:1:19718)
    at stackTrace (/usr/local/lib/node_modules/truffle/node_modules/solc/soljson.js:1:19901)
    at abort (/usr/local/lib/node_modules/truffle/node_modules/solc/soljson.js:19:15285)
    at Array.dXb (/usr/local/lib/node_modules/truffle/node_modules/solc/soljson.js:11:585101)
    at Array.tX (/usr/local/lib/node_modules/truffle/node_modules/solc/soljson.js:8:557453)
    at Array.eea (/usr/local/lib/node_modules/truffle/node_modules/solc/soljson.js:6:495332)
    at Object.wOb [as dynCall_ii] (/usr/local/lib/node_modules/truffle/node_modules/solc/soljson.js:11:540050)
    at invoke_ii (/usr/local/lib/node_modules/truffle/node_modules/solc/soljson.js:1:1275129)
    at Array.ixa (/usr/local/lib/node_modules/truffle/node_modules/solc/soljson.js:7:494308)
    at Object.bVb [as dynCall_viii] (/usr/local/lib/node_modules/truffle/node_modules/solc/soljson.js:11:577232)
If this abort() is unexpected, build with -s ASSERTIONS=1 which can give more information.

I'm following this reference: https://learnxinyminutes.com/docs/solidity/

4

Define your const variables outside of your functions:

uint constant a = 8;  

function basicSyntax() returns(uint) {
    return 1;
}

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.