I am doing a simple HelloWorld class trying out several things including emitting events. My contract code is below:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HelloWorld {
string public myMessage = "Hello World!";
event MessageReturned( string message );
function setMessage( string memory newMessage ) external {
myMessage = newMessage;
}
function getMessage() external view returns( string memory ) {
return myMessage;
}
function sayMessage() external view {
emit MessageReturned( myMessage );
}
}
I got surprised I got a TypeError when I tried to compile with truffle compile. I received this message form solc:
TypeError: Function cannot be declared as view because this expression (potentially) modifies the state.
--> project:/contracts/HelloWorld.sol:18:14:
|
18 | emit MessageReturned( myMessage );
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Could anyone please help explain to me why emitting an event is treated as a state change here? What goes behind the scenes to cause it to mine?