Define rules for writing custom getters for public functions #3780
Labels
closed due inactivity
The issue/PR was automatically closed due to inactivity.
stale
The issue/PR was marked as stale because it has been open for too long.
As I understand, every public state variable will have a corresponding external getter function with the same name (but can have necessary input parameters for some cases).
My question is what if I have a function with the same signature (function name and input parameter types) as this auto-generated getter function?
I've experimented with the following simple contract in Remix, and I found the explicitly defined function (here
number()
) seems to shadow the auto-generated getter function.Here I found the log output of function
print()
are 111 and 222. If I change the signature of functionnumber()
(e.g., add an unused input parameterbool unusend
), the result will be both 111 as I expected, since the getter and explicitly defined function now have different signatures.What's more, if I move the declaration of
uint256 number
to the end of the contract body (so it is after the declaration offunction number()
), I will be given a TypeError foremit log(number)
and a DeclarationError foremit log(this.number());
. It seems when dealing withemit log(number);
, Solidity thinks herenumber
is a function and so give a TypeError; and when seenuint256 public number = 111;
, it thinks there's already an attribute namednumber
(the function) and so give a DeclarationError.So, my question is: should there be constraints between state variable names and function names in Solidity, especially when public state variables have their auto-generated getter functions? If so, what is the precise rule and where can I find it in any documentation.
The text was updated successfully, but these errors were encountered: