2

A function with the same name as the contract is normally a constructor and it’s bytecode isn’t included in the resulting contract.

But is there a way to declare a function with the same name as the declared contract name so it’s both a constructor and a function inside the resulting contract with a signature in the function selector bytecode ?

If yes, was it introduced recently or is it part of the language nearly since the beginning ?

5
  • 1
    I have doubts this answer is true. I’m unwilling to spend money on a transaction to check it. Commented May 28, 2018 at 20:42
  • 1
    So test it using the javascript vm in remix.ethereum.org. It won't cost you anything, but it uses the same compiler as you would use to compile smart contracts for the mainnet.
    – Henk
    Commented May 28, 2018 at 21:00
  • @user2284570, as mentioned, just use the javascript vm or a testnet. That said, I've just done this and you are correct that the answer is wrong.
    – mafrasi2
    Commented May 28, 2018 at 21:30
  • @mafrasi2 do you also tested with 0.4.24 ? Commented May 28, 2018 at 21:55
  • yes, that was the version I tested this on
    – mafrasi2
    Commented May 28, 2018 at 22:02

1 Answer 1

4

In Solidity version 0.4.24 declaring a function with the contracts name as as constructor has been depreciated in favor for constructor()

If you are using an older version of Solidity than 0.4.24, you declare the function as function contractName().

Similar to this answer constructors are called once, and only once. They are initiated once a contract has been published, and cannot be called again.

When attempting to use constructor() and function contractName() in the same contract, it detects two constructors resulting in DeclarationError: More than one constructor defined. Meaning that the EVM can detect both as acting constructors and will not compile. Implying function contractname() even when declared as public cannot be referred to as a function.

4
  • The answer you linked to says pretty much the opposite, but it's wrong and your answer is correct.
    – mafrasi2
    Commented May 28, 2018 at 21:40
  • @O.Privacy do you mean It’s possible since 0.4.24 ? Commented May 28, 2018 at 21:53
  • @mafrasi2 Thanks for letting me know, linked the wrong one.
    – Connor
    Commented May 28, 2018 at 22:02
  • Hello from the future, in version 0.5.0, using function contractName() now throws an error. Commented Jan 23, 2019 at 1:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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