I received an error by the compiler regarding the following solidity code and I would like to know what's this error and what's the fix?

    interface tokenRecipient { function   receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external; }

Error in

    TypeError: Data location must be "calldata" for parameter in external function, but none was given.
    interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external; }                                                                                                                                                                                       
  • It gives no error in Remix at least. Where do you get the error? – Lauri Peltonen Sep 28 '18 at 10:27
  • In Remix compiler version:0.5.0 – hibanl Sep 28 '18 at 11:11

There are some breaking changes in Solidity version 0.5. More details at: https://solidity.readthedocs.io/en/latest/050-breaking-changes.html

One of these breaking changes is:

Explicit data location for all variables of struct, array or mapping types is now mandatory. This is also applied to function parameters and return variables. For example, change uint[] x = m_x to uint[] storage x = m_x, and function f(uint[][] x) to function f(uint[][] memory x) where memory is the data location and might be replaced by storage or calldata accordingly. Note that external functions require parameters with a data location of calldata.

This explicitly states that if your function is external the location type must be calldata - but the location is only required for structs, arrays and mappings. As bytes is almost the same as byte[] it is considered as an array (https://solidity.readthedocs.io/en/v0.4.21/types.html#arrays).

If you're wondering what is calldata, this is also explained at https://solidity.readthedocs.io/en/v0.4.21/types.html#data-location

There is also a third data location, calldata, which is a non-modifiable, non-persistent area where function arguments are stored. Function parameters (not return parameters) of external functions are forced to calldata and behave mostly like memory.

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.