1

From Solidity tutorials:

contract C {
    uint[] counters;
    function getCounter(uint index)
        returns (uint counter, bool error) {
            if (index >= counters.length) return (0, true);
            else return (counters[index], false);
        }
    function checkCounter(uint index) {
        var (counter, error) = getCounter(index);
        if (error) { ... }
        else { ... }
    }
}

If I compile this with truffle, a warning is displayed:

Warning: Use of the "var" keyword is deprecated.

What is ther alternate to removing var when receiving tupples from returning functions? I tried removing var but truffle refuses from compiling with error.

5

You can define the variable first with their type and then assign to the tuple.

function checkCounter(uint index) {
    uint counter;
    bool error;
    (counter, error) = getCounter(index);
    if (error) {  }
    else {  }
}
2

Variables have to be strictly typed now. So you have to declare the variables first:

contract C {
    uint[] counters;
    function getCounter(uint index)
        returns (uint counter, bool error) {
            if (index >= counters.length) return (0, true);
            else return (counters[index], false);
        }
    function checkCounter(uint index) {
        uint counter;
        bool error;

        (counter, error) = getCounter(index);
        if (error) { ... }
        else { ... }
    }
}
0
// You have only to use solidity syntax, you can'nt mixed with javascript syntax
// 'var' is not accepted 

contract Counters {

  uint[] counters;

  function _getCounter(uint _index) view private returns (uint, bool success) {
    require(_index >= 0 && _index < counters.length);
    return (counters[_index], true); // Return the value on that position in the array
  }

  function checkCounter(uint _index) view public returns ( ... ) {
    uint counter;
    bool success;
    (counter, success) = _getCounter(_index);
    if (success == false) { ... } else { ... };
  }

}
2
  • var was accepted in previous versions of solidity
    – Karan
    Apr 13 '18 at 10:35
  • var was legit in the previous versions and still used in the documentation as reference. Its not javascript syntax solidity.readthedocs.io/en/v0.4.21/… . now is deprecated but the code will still compile with warnings
    – mirg
    Apr 13 '18 at 10:38

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.