Solidity:这些句子是否相同?或者它们的意思不同?

Solidity: Are these sentences the same? Or do they mean different things?

为了调用一个函数'isContract',参数'to'是一个地址,两种方式都有效吗? :

   to.isContract() 

   isContract(to)

Solidity 是否允许两种方式? 我在不同的代码中找到了两者,我不知道 'isContract(to)' 是否正确,或者 'to.isContract()' 是否意味着另一个不同的东西。

非常感谢您的帮助。

它们不一样。

to.isContract() 表明您已经定义了一个接口(在您的代码中),该接口定义了一个 isContract() 函数,并且部署在 to 地址的合约实现了该接口。

pragma solidity ^0.8.0;

interface ExternalContract {
    function isContract() external returns (bool);
}

contract MyContract {
    function foo() external {
        ExternalContract to = ExternalContract(address(0x123));
        bool returnedValue = to.isContract(); // calling `to`'s function `isContract()`
    }
}

isContract(to) 在您的合约中调用 externalinternal 函数。

pragma solidity ^0.8.0;

contract MyContract {
    function foo() external {
        address to = address(0x123);
        bool returnedValue = isContract(to); // calling your function `isContract()`
    }
    
    function isContract(address to) internal returns (bool) {
        return true;
    }
}

编辑:我又忘记了一个案例——使用一个包含 isContract() 函数的库作为地址。示例 OpenZeppelin 实现:definition, usage.

library AddressLibrary {
    function isContract (address _address) {
        return true;
    }
}

contract MyContract {
    using AddressLibrary for address;

    function foo() external {
        address to = address(0x123);
        bool returnedValue to.isContract(); // calling your function `isContract(to)`
    }