1

While compiling in truffle I got an error because of payable modifier. If I remove payable then there's no error. But I want to use payable. Can anyone please help me?

error:

truffle(develop)> compile
Error parsing /contracts/test.sol: ParsedContract.sol:3:13: ParserError: Expected identifier but got 'payable'
    address payable []array;

contract is:

pragma solidity >=0.4.0 <0.6.0;
contract test{
    address payable []array;
}
  • AFAIK only a function can be payable, not a field. – ivicaa Jan 12 at 17:58
  • 1
    @ivicaa: Starting from solc 0.5.x (used in Truffle 5.x onward as the default compiler), a state variable (of type address if I recall correctly) can be declared payable. – goodvibration Jan 12 at 18:19
  • @goodvibration ah! Got it. Payable address has the transfer function. – ivicaa Jan 12 at 18:51
1

The payable modifier for addresses is only available from solc v0.5.xx. It is a measure to ensure safe use of transfer when only it is needed.

This compiles and work with solc v0.5.xx and fails with v0.4.xx.

pragma solidity >=0.4.0 <0.6.0;
contract test{
    address payable []array;
}

If you target solc v0.4.xx then it is not required and the code should work without it. The only disadvantge is the compiler will not warn you when you make a transfer.

This programs works with solc v0.4.xx.

pragma solidity >=0.4.0 <0.6.0;
contract test{
    address []array;
}
  • That does not provide an to the question though (why the compilation error). – goodvibration Jan 12 at 20:09
  • @goodvibration It compiles correctly with solc v0.5 and fails with solc v0.4 because it is an invalid syntax. – Ismael Jan 13 at 3:52

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.