0

Both,

struct Test {
  uint x;
  uint y;
}

contract A {
  Test test;
}

and

contract A {
  struct Test {
    uint x;
    uint y;
  }

  Test test;
}

are valid ways to define a struct. Is there any difference between both methods?

1 Answer 1

2

Basically if you define struct outside the contract you are making it public and every contract which iport the .sol file could use it where as if it is in the contract scope it is private and only the contract itself could use it. Also if it is outside you cold have problems with same named contract.

Example:

struct Test { ... }
contract Test { ... }
1
  • How would you query storage, on an offchain file, from a struct declared outside a contract and which variables were assigned on another contract?
    – dNyrM
    Nov 15, 2021 at 22:52

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.