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

Getters returning structs can not be defined in interfaces #11826

Closed
axic opened this issue Aug 20, 2021 · 8 comments
Closed

Getters returning structs can not be defined in interfaces #11826

axic opened this issue Aug 20, 2021 · 8 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

@axic
Copy link
Member

axic commented Aug 20, 2021

interface I {
    struct S {
        uint a;
    }

    function s() external returns (S memory);
}

contract C is I {
    S public override s;
}

The function s can not be defined in a way this works. The problem lies in the impossibility to define a working location.

S storage is not allowed in interfaces, but that could be one option:

Error: Data location must be "memory" or "calldata" for return parameter in function, but "storage" was given.
 --> struct-interface.sol:6:36:
  |
6 |     function s() external returns (S storage);
  |                                    ^^^^^^^^^

S memory is not allowed by overriding, but that could be equally correct:

Error: Overriding public state variable return types differ.
  --> struct-interface.sol:10:5:
   |
10 |     S public override s;
   |     ^^^^^^^^^^^^^^^^^^^
Note: Overridden public state variable is here:
 --> struct-interface.sol:6:5:
  |
6 |     function s() external returns (S memory);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@axic
Copy link
Member Author

axic commented Aug 20, 2021

Oops, forgot that struct getters return the members, and not the struct itself. That is super unintuitive.

@leonardoalt
Copy link
Member

Oops, forgot that struct getters return the members, and not the struct itself. That is super unintuitive.

Don't forget that if the struct has arrays those are ignored.

@axic
Copy link
Member Author

axic commented Aug 20, 2021

Error: Overriding public state variable return types differ.
  --> struct-interface.sol:10:5:
   |
10 |     S public override s;
   |     ^^^^^^^^^^^^^^^^^^^

Could also consider displaying the return types in this error.

@ekpyron
Copy link
Member

ekpyron commented Aug 20, 2021

Oops, forgot that struct getters return the members, and not the struct itself. That is super unintuitive.

Can we change that at some point :-)?

@cameel
Copy link
Member

cameel commented Aug 20, 2021

Even weirder that this goes only one level deep. You can have a getter returning a struct if it's nested inside another struct.

For example this compiles just fine and both the function and the getter return S:

interface I {
    struct S {
        uint a;
    }
    struct WrappedS {
        S s;
    }

    function s() external returns (S memory);
}

contract C is I {
    WrappedS public override s;
}

Anyway, you should update issue the description to say what we want to do about it. Do we want to make getters return structs instead of tuples (breaking change I guess?). Or just make the message more explicit?

@abhijitgawai
Copy link

@axic How to write Interface for such view function. What should be data type

@github-actions
Copy link

This issue has been marked as stale due to inactivity for the last 90 days.
It will be automatically closed in 7 days.

@github-actions github-actions bot added the stale The issue/PR was marked as stale because it has been open for too long. label Mar 21, 2023
@github-actions
Copy link

Hi everyone! This issue has been automatically 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 Mar 28, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 28, 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

5 participants