9

It seems that the purpose of function foo() constant {} is to indicate that a function does not change the contract's state. And yet, as it states here, the compiler doesn't actually enforce this. So for the time being, is constant simply an unenforced convention? A way to let contract readers know that this function shouldn't change state?

In reality, this bar function works just fine:

contract Foo {
  uint x;

  function bar(uint y) constant {
    x = y;
  }
}
7

As noted in the question, constant isn't enforced by the compiler yet.

But it is used by web3.js to do a call instead of sendTransaction.

So in the question's example, contractInstance.bar(2) would run the equivalent of contractInstance.bar.call(2, ...).

If bar was not marked constant, contractInstance.bar.sendTransaction(2, ...) would be executed instead.

See Contract Methods and Difference between a transaction and call for more information.

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.