[Ethereum] Why public variables aren’t able to implement interface functions

interfacessoliditystate-variable

I'm curious why interfaces don't accept public variables as implementations, since public variables have getter functions generated for them.

For example why couldn't the ERC20 balanceOf interface function be implemented by a public balanceOf variable.

interface

function balanceOf(address tokenOwner) public constant returns (uint256 balance);

implementing contract

mapping (address => uint256) public balances;

Best Answer

Mostly, it's for two main reasons:

1) Functions aren't variables and they are not interchangeable

2) Functions support a lot more things than variables do. Actually variables don't support any functionality, just their (convenience) getters.

Functions and variables behave quite differently. You have to implement the interface's functions, which literally means that they have to have an implementation - they have to do something. A getter doesn't really do anything else than return the value of a variable.

For example your interface function balanceOf: nobody says exactly how you should implement the function - it just needs to have the given signature and the rest is up to you. Nobody tells you it has to return any meaningful value - maybe you decide to always return 0 and it's totally fine from the interface's point of view. Or maybe you want to do complicated calculations - you can't do any of that with just a getter function.

I found someone else also having the same idea: https://github.com/ethereum/solidity/issues/3514