Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define rules for writing custom getters for public functions #3780

Closed
chaoliu92 opened this issue Mar 24, 2018 · 3 comments
Closed

Define rules for writing custom getters for public functions #3780

chaoliu92 opened this issue Mar 24, 2018 · 3 comments
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.

Comments

@chaoliu92
Copy link

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.

pragma solidity ^0.4.21;

contract TestFunctionStateVariableNameConflict {
    event log(uint256 number);
    uint256 public number = 111;
    function number() public pure returns(uint256) {
        return 222;
    }
    function print() public {
        emit log(number);  // This will output value 111 (as the state variable)
        emit log(this.number());  // This will output value 222 (as the function number())
    }
}

Here I found the log output of function print() are 111 and 222. If I change the signature of function number() (e.g., add an unused input parameter bool 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 of function number()), I will be given a TypeError for emit log(number) and a DeclarationError for emit log(this.number());. It seems when dealing with emit log(number);, Solidity thinks here number is a function and so give a TypeError; and when seen uint256 public number = 111;, it thinks there's already an attribute named number (the function) and so give a DeclarationError.

pragma solidity ^0.4.21;

contract TestFunctionStateVariableNameConflict {
    event log(uint256 number);
    // uint256 public number = 111;
    function number() public pure returns(uint256) {
        return 222;
    }
    function print() public {
        emit log(number);  // This will lead to a TypeError
        emit log(this.number());
    }
    uint256 public number = 111;  // This will lead to 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.

@chaoliu92 chaoliu92 changed the title Public state variable with the same name (or signature) as a public (or external) function Public state variable with the same name (or signature) as an explicitly defined public (or external) function Mar 24, 2018
@chriseth
Copy link
Contributor

We are currently working on a proper inheritance and shadowing specification: #3698

@chriseth
Copy link
Contributor

Sorry, the correct issue is #3729

@Marenz Marenz changed the title Public state variable with the same name (or signature) as an explicitly defined public (or external) function Define rules for writing custom getters for public functions Jan 30, 2020
@NunoFilipeSantos NunoFilipeSantos added the stale The issue/PR was marked as stale because it has been open for too long. label Nov 25, 2022
@github-actions
Copy link

Hi everyone! This issue has been closed due to inactivity.
If you think this issue is still relevant in the latest Solidity version and you have something to contribute, feel free to reopen.
However, unless the issue is a concrete proposal that can be implemented, we recommend starting a language discussion on the forum instead.

@github-actions github-actions bot added the closed due inactivity The issue/PR was automatically closed due to inactivity. label Jan 31, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
Projects
None yet
Development

No branches or pull requests

3 participants