Skip to content

Instantly share code, notes, and snippets.

@cryppadotta
Last active September 26, 2022 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cryppadotta/e84c2c9a87d32cc51f77433824865509 to your computer and use it in GitHub Desktop.
Save cryppadotta/e84c2c9a87d32cc51f77433824865509 to your computer and use it in GitHub Desktop.
pragma solidity ^0.8.6;
contract SimpleStorage {
struct A {
uint256 id;
}
struct B {
A[] ids;
bool active;
}
B[] public bs;
mapping(uint256 => B) public bstorage;
constructor() public {
/// @dev try #1:
/// @notice TypeError: Invalid type for argument in function call. Invalid implicit conversion from struct SimpleStorage.A[2] memory to struct SimpleStorage.A[] memory requested
// bs[0] = B({active: true, ids: [A({id: 1}))]});
/// -
/// @dev try #2:
/// @notice TypeError: Member "push" is not available in struct SimpleStorage.A[] memory outside of storage.
// A[] memory a;
// a.push(A({id: 1}));
// bs[0] = B({active: true, ids: a});
/// -
/// @dev try #3:
/// @notice Copying of type struct SimpleStorage.A memory[] memory to storage not yet supported.
// A[] memory a = new A[](1);
// a[0] = A({id: 1});
// bs[0] = B({active: true, ids: a});
/// -
/// @dev try #4:
/// @notice UnimplementedFeatureError: Copying of type struct SimpleStorage.A memory[] memory to storage not yet supported.
// B memory b = B(new A[](1), true);
// bs.push(b);
/// -
/// @dev try #5
/// @notice this actually works
{
B storage b = bs.push();
b.active = true;
for (uint256 i = 0; i < 2; i++) {
b.ids.push(A({id: i}));
}
}
/// what if we want to use a mapping?
/// @dev try storage #1
/// @notice TypeError: Invalid type for argument in function call. Invalid implicit conversion from struct SimpleStorage.A[1] memory to struct SimpleStorage.A[] memory requested.
// bstorage[0] = B({active: true, ids: [A({id: 1})]});
/// @dev try storage #2
/// @notice UnimplementedFeatureError: Copying of type struct SimpleStorage.A memory[] memory to storage not yet supported.
// A[] memory a = new A[](1);
// a[0] = A({id: 1});
// bstorage[0] = B({active: true, ids: a});
/// @dev try storage #3
/// @notice This works
B storage b_a = bstorage[0];
b_a.active = true;
/// nope
/// @notice UnimplementedFeatureError: Copying of type struct SimpleStorage.A memory[] memory to storage not yet supported.
// A[] memory a = new A[](1);
// a[0] = A({id: 1});
// b_a.ids = a;
b_a.ids[0] = A({id: 1});
b_a.ids[1] = A({id: 2});
/// @dev try storage #5
/// @notice this works too
{
bstorage[0].active = true;
bstorage[0].ids[0] = A({id: 1});
}
}
}
@wagmiwiz
Copy link

first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment