1

In other words if I have a contract called "mycontract" and then simply add the same function with the same name "mycontract" as public and payable, will this mean that others can call it?

Since I tried this in remix but the function with the same name doesn't appear in the UI, I guess this means it's constructor and cannot be called more than once (after contract creation)? Thanks!

1

There is some confusion in your question. The constructor is, by definition, a special function called during deploy (as last thing done) and, in general, it contains initialization code for the rest of the contract.

The constructor is automatically called before to declare “deployed” the contract and it is not stored on blockchain: it is a once only, deploytime only, function.

In particular you cannot find any code from constructor if you watch at the bytecode of any already deployed contract: it simply does not exist anymore.

On the contrary the fallback function is simply a function without name that the system calls when any call for a named function that does not exist arrives to the contract. In that sense, if your contract DOES NOT have a function foo() if you try to call a function named foo in your contract, by default the fallback will be called.

In the same way, if ether are sent to your contract without any valid function name call in the transaction, the contract execs your fallback.

Given this, the “constructor” keyword substitutes the old convention (up to 0.4.22 compiler version if I’m not wrong) that was to declare the constructor using as function name the same name of the contract. Now this is no more working and you have to declare “constructor” it.

1

A payable modificator doesn't mark the function as fallback. Fallback function is a default function without a name. So function with a name is not a fallback. Constructor unavailable for call after deployment.

  • In other words, I cannot call this? I guess since it doesn't appear in remix... – kpopguy Feb 4 at 14:00
  • Yes, because it is not a function. This is a piece of code that runs when you deploy. – Ivan Zakharov Feb 4 at 14:37
1

From Solidity version 0.5 documentation (https://solidity.readthedocs.io/en/v0.5.0/050-breaking-changes.html#constructors)

Constructors must now be defined using the constructor keyword.

However, I'm unsure whether this means whether it would be considered as a regular function. I'd be at least heavily discouraged and probably forbidden in UIs and some compilers.

So: don't try to use payable constructor function as a fallback. Constructors are only called exactly once when the contract is initialized and they can't be fallbacks. Even if it might be treated as a regular function in some environments, it wouldn't be considered as the fallback function as that function has never explicit name.

  • Thanks, but does this mean I am unable to call any function with name the same as the contract's name? – kpopguy Feb 4 at 14:00
  • 1
    As I said, I'm unsure of that part. In theory it might work but there may come practical difficulties (such as Remix not displaying the function). But, in the end, there's zero reason to try such a thing, so just choose another name for your functions. – Lauri Peltonen Feb 4 at 14:02
  • 1
    You're correct, no need to make it too complex...I'll go the standard way. – kpopguy Feb 4 at 14:03
0

You always can create a separate function and call it from constructor and your fallback

contract MyContract {

    constructor(uint a, address b, bool c) public {
        _init(a, b, c);
    }

    function () public {
        _init(msg.value, msg.sender, false);
    }

    function _init(uint a, address b, bool c) private {
        /* Do something */
    }
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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