In defi contracts, I see codes like this
address public constant dai = address(0x6B175474E89094C44Da98b954EedeAC495271d0F);
What's the point of casting an address to address? Or is this just messy code? Thanks
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.Sign up to join this community
The type of value
0x6B175474E89094C44Da98b954EedeAC495271d0F is actually address literal (https://docs.soliditylang.org/en/v0.7.0/types.html#address-literals) which is not exactly the same as an address - although it is easy to convert between these two.
address public constant dai = 0x6B175474E89094C44Da98b954EedeAC495271d0F; works as well but under the hood Solidity is casting it into an address as it recognizes that it looks like an address (and because you are setting it to an address type).
So you can basically choose. Doing an explicit cast makes the intention clearer - it's often good to be as clear as possible when writing Solidity code.