5

I was struck by this Solidity 0.6.0 breaking change:

Change address(f) to f.address for f being of external function type.

Not a big deal, but I'm clearly very ignorant here. 2 stupid questions. Q1: I use

contractAddress.f()

a lot, where 'contractAddress' is the contract's Ethereum address, and f is some external function in that contract. Does this imply I should change my code to write

f.contractAddress()

instead? Q2: Does this mean I should change

address(this)

to

this.address

?

1
  • I don't know Solidity 0.6.0, but I find it hard to believe that you should do f.contractAddress(), where f is the function name and contractAddress is the contract instance. It would be pretty much the opposite of the convention in any other language of sort. – goodvibration Dec 27 '19 at 21:09
3

This change is about external function types. As you can read in the Breaking Changes:

Conversions from external function types to address are now disallowed. Instead external function types have a member called address, similar to the existing selector member.

First of all, as the docs says:

External functions consist of an address and a function signature and they can be passed via and returned from external function calls.


Solidity v0.5.15

In Solidity docs (v0.5.15) about Function Types (Conversions) you can read the following:

A value of external function type can be explicitly converted to address resulting in the address of the contract of the function.

Code (taken from the Oracle example):

pragma solidity 0.5.15;

contract Test {

    struct Request {
        function(uint) external callback;
    }

    Request[] private requests;

    function getAddress (uint requestID) public view returns (address) {
        return address(requests[requestID].callback);
    }
}

Solidity v0.6.0

The conversion is now disallowed. If you try the code above you will see something like that:

enter image description here

We must update the code like that:

Change address(f) to f.address for f being of external function type.

If you go to Solidity docs (v0.6.0) and read about Function Types (Members) there is a new member:

  • .address returns the address of the contract of the function.

Code (taken from the Oracle example):

pragma solidity 0.6.0;

contract Test {

    struct Request {
        function(uint) external callback;
    }

    Request[] private requests;

    function getAddress (uint requestID) public view returns (address) {
        return requests[requestID].callback.address;
    }
}

With the code examples I just want to illustrate the compilation error.


Answering your questions:

Q1: No. Calling functions works in the same way.

Example:

pragma solidity 0.6.0;

contract Contract1 {

    mapping (address => uint256) votes;

    function voteCandidate(address _address) external {
        votes[_address]++;
    }

    function checkVotesCandidate(address _address) public view returns (uint) {
        return votes[_address];
    }
}

contract Caller1 {

    Contract1 c1;

    constructor(address _address) public {
        c1 = Contract1(_address);
    }

    function voteCandidate() public {
        c1.voteCandidate(msg.sender);
    }
}

Q2: If you use this.address you will get a compilation error because it was deprecated in Solidity v0.5, so you must use address(this).

1
  • That's a great answer. I was looking for this as well. Good job. – Niklas Feurstein Dec 29 '19 at 12:59

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.