4

Suppose we have a contract Foo and another contract Bar that receives the address of some instance of Foo in its constructor.

Is there a difference (in functionality or gas usage) between doing:

function Bar(address _foo) public {
  foo = Foo(_foo)
}

and

function Bar(Foo _foo) public {
  foo = _foo
}
  • They produce the exact same EVM byte code. However, the compiler checks the ABI if you use contract type instead of address type – libertylocked Apr 5 '18 at 21:20
3

Functionally: no difference.

Gas usage: no difference.

You can think of static types as hints for the compiler. At run-time, there is no difference.

You can easily verify/trace this in remix with this code:

contract X {
    uint x = 100;

    function doSomething() public {
        x++;    
    }
}

contract Y {
    function a(address _x) public {
        X x = X(_x);
        x.doSomething();
    }
}

contract Z {
    function a(X _x) public {
        X x = _x;
        x.doSomething();
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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