2

I am following uniswap v3 development book. We have a code:



pool = address(
    new UniswapV3Pool{
        salt: keccak256(abi.encodePacked(tokenX, tokenY, tickSpacing))
    }()
);

My question is, is it necessary to typecast it to address?? when we are doing new UniswapV3Pool was not it enough to transform it in address? as UniswapV3Pool is a contract and here we are making a new instance of contract. And also solidity doc saying this: D d = new D{salt: salt}(arg); from here.

1 Answer 1

0

Yes, in your case it is necessary because if you create a new instance of a contract using the new keyword in Solidity, the type of the created instance is the contract type itself, not an address that would be a problem.

here is an example:

pragma solidity ^0.8.0;

contract MyContract {
    // ...
}

contract AnotherContract {
    address public myContractInstance;

    function createMyContract() public {
        // This would result in a type error because the type of 'new MyContract()' is 'MyContract', not 'address'
    
        // This would work because you are explicitly typecasting 'MyContract' to 'address'
        myContractInstance = address(new MyContract());
    }
}

typecasting new UniswapV3Pool{...}() to address is necessary to store the created instance in the pool variable, which is of type address

2
  • actually the function returning returns(address pool), i think as pool is type of address then we had to type cast it to address
    – Hacek
    Apr 14, 2023 at 14:37
  • Yes, the type of the created instance is the contract type itself, not an address.
    – Saxtheowl
    Apr 14, 2023 at 18:29

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.