我在混音中收到警告。这是什么意思?我是不是漏掉了什么?下面是演示它的简单代码:
pragma solidity ^0.4.20;
contract HelloWorld
{
string hello = "Hello World";
function sayHello() returns (string)
{
return hello;
}
}现在我收到警告:
browser/HelloWorld.sol:6:5: Warning: Function state mutability can be restricted to view
function sayHello() returns (string)
^ (Relevant source part starts here and spans across multiple lines).发布于 2018-03-11 15:26:58
这就意味着,如果您将函数状态设置为查看,它“知道”该函数只从块链读取,不会更改任何变量。任何事情都不需要改变,因此汽油费会稍微低一点。
发布于 2019-06-02 17:30:21
在函数中使用view,如果您希望函数读取传递的variable.using视图,该函数将不会改变存储状态,但同时将读取值。
pragma solidity ^0.5.0;
contract HelloWorld
{
string hello = "Hello World";
function sayHello() public view returns (string memory)
{
return hello;
}
}https://ethereum.stackexchange.com/questions/42396
复制相似问题