简体   繁体   中英

What's the meaning of this warning in solidity?

When I was writing my code I got warning at 10th line of my code. Can anyone tell me what's this warning means?

My Code

// SPDX-License-Identifier: UNLICENSED

pragma solidity >=0.5.0 < 0.9.0;

contract PracticeTest // It's a class
{
    string name ;
    uint256 age;

    constructor() public
    {
        name = "Ali";
        age = 21 ;
    }
}

This is the Warning

Warning: Visibility for constructor is ignored. If you want the contract to be non-deployable, making it "abstract" is sufficient.
  --> contracts/PracticeTest.sol:10:5:
   |
10 |     constructor() public
   |     ^ (Relevant source part starts here and spans across multiple lines).

Visibility ( public / internal ) is not needed for constructors anymore: To prevent a contract from being created, it can be marked abstract . This makes the visibility concept for constructors obsolete.

Source: https://docs.soliditylang.org/en/v0.8.13/070-breaking-changes.html#functions-and-events


So, if you're compiling your contract with Solidity version 0.7 or newer, the constructor visibility (in your case public ) is ignored, and you can safely remove it.

constructor()
{
    name = "Ali";
    age = 21 ;
}

I am just adding to the answer of Petr Hejda . Prior to solidity compiler versions 0.7.0 we were required to provide the visibility of constructors as either public or internal . With public visibility the contract can be directly deployed by itself on the blockchain whereas if the contract is not intended to be created directly but rather inherited, then we declared the constructor with internal visibility.
However, for solidity compiler versions 0.7.0 or greater we do the same thing but differently. If we want the contract not to be deployed directly but inherited we declare the contract itself as abstract . If we want that the contract can be deployed directly and can also be inherited, we don't declare it as abstract contract and we don't specify any visibility in the constructor of that class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM