0

I have an array which is made up of user-defined value types:

type UD60x18 is uint256;
UD60x18 memory foo[] = new UD60x18[](2);

Now, I would like to turn this into uint256. Can I cast this array to uint256[] without creating another variable?

2
  • According to this comment (github.com/ethereum/solidity/issues/…) it doesn't seem possible for now... It's only one line to do that conversion in assembly though.
    – hroussille
    Commented Jun 14, 2022 at 13:05
  • @hroussille do you know how to do that in assembly? I wouldn't mind using Yul. Commented Jun 14, 2022 at 14:28

1 Answer 1

1

To answer your comment, the following function would do that conversion using assembly :

// Since UD60x18 is uint256 their underlying size and representation are the same
// Just make a uint256[] reference point to your UD60x18[] memory variable
function unwrapArray(UD60x18[] memory foo) internal pure returns (uint256[] memory bar) {
    assembly {
        bar := foo
    }
}

Thus, this would return a uint256[] with values [1, 2] :

function test() public pure returns (uint256[] memory) {
    UD60x18[] memory foo = new UD60x18[](2);
    foo[0] = UD60x18.wrap(1);
    foo[1] = UD60x18.wrap(2);
    return unwrapArray(foo); // [1, 2]
}

It does create a new variable though... As far as I know of, there is no implicit or explicit conversion allowed for used defined value types currently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.