I have previously posted this request as an issue on Solc GitHub, but it has not been addressed to completion, so I’d like to bring it onto a broader discussion in this forum.
Consider this, for example:
library BigNumber {
function add(uint256[] memory x, uint256[] memory y) internal pure returns (uint256[] memory z) { ... }
function sub(uint256[] memory x, uint256[] memory y) internal pure returns (uint256[] memory z) { ... }
function mul(uint256[] memory x, uint256[] memory y) internal pure returns (uint256[] memory z) { ... }
...
}
Since the type
keyword does not support non-primitive types such as uint256[]
, the only alternative is to “wrap” that type inside a structure:
struct Obj {
uint256[] data;
}
library BigNumber {
function add(Obj memory x, Obj memory y) internal pure returns (Obj memory z) { ... }
function sub(Obj memory x, Obj memory y) internal pure returns (Obj memory z) { ... }
function mul(Obj memory x, Obj memory y) internal pure returns (Obj memory z) { ... }
...
}
However, uint256[]
and struct Obj { uint256[] data; }
do not share the exact same memory-layout, and ultimately, the solution above yields a higher gas cost.
So ideally, the type
keyword should also support non-primitive types.