1

What happens if a function is being passed before { into another function?

Like DetailedERC20(_name, _symbol, TOKEN_DECIMALS) in this example:

function MyToken(Registry _registry, string _name, string _symbol) public
    DetailedERC20(_name, _symbol, TOKEN_DECIMALS)
{
    require(_registry != address(0));

    registry = _registry;
}

Would this be the same as above:

function MyToken(Registry _registry, string _name, string _symbol) public
{
    DetailedERC20(_name, _symbol, TOKEN_DECIMALS)
    require(_registry != address(0));

    registry = _registry;
}
1

This is a constructor in the old syntax. The name, MyToken matches the name of the contract and this is therefore considered the constructor. Like this:

contract MyToken {
  function MyToken( ...

This is the old system. This caused some errors on the part of developers, so more recent compilers use the constructor keyword, like this:

contract MyToken {
  constructor(...

Construtors are like init() in other languages. They run exactly one time during deployment. They cannot run again and because of this, the constructor bytecode isn't even stored.

The pattern you see also uses inheritance and arguments passed into constructors. Suppose you have a contract DetailedERC20 and it's constructor takes some arguments (_name, _symbol, TOKEN_DECIMALS)

You also have MyToken and it inherits from the first, like:

contract MyToken is DetailedERC20 { ...

MyToken has a constructor that is taking some arguments. Some of those arguments need to passed through to DetailedERC20's constructor so it will know what to do.

The first set of arguments (Registry _registry, string _name, string _symbol) is declaring the function signature. Notice all the variables are typed. The second one is scooping up some of the defined inputs and passing them into the inherited constructor.

There is another example/explanation over here: Inherited Constructors

Hope it helps.

  • 1
    So Inherited Constructor is the keyword. Thank you very much, that makes sense to me! Would an inherited constructor look in the new syntax the same? – saitam Nov 14 '18 at 18:42
  • Is the inherited constructor look used in the new syntax the same way as in the old one? – saitam Nov 14 '18 at 18:48
  • Only the word changed. The implied constructor was a function with the same name as the contract. The new method is the keyword constructor. – Rob Hitchens Nov 14 '18 at 18:49
  • I understand that, but the inherited contract is still initialized with its own name? So in my example with constructor(_var1, _var2) public NameOfInheritedContr (_var1, _var2) { ... } – saitam Nov 14 '18 at 18:53
  • 1
    I get you, now. Yes. The functions are all called function constructor(). The inheritance arguments syntax uses the name of the contract. – Rob Hitchens Nov 14 '18 at 18:56

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.