Skip to content
LogoLogo

Reading and writing state

State is read and written through storage.. A function's keyword says whether it may write, and the compiler holds it to that.

state.lithe
import "std/evm";
 
storage is record {
  value : uint;
  updatedBy : address;
}
 
(* A stateful external function may write storage. *)
external function set(const value : uint) is {
  storage.value := value;
  storage.updatedBy := Evm.sender();
}
 
(* A view may read storage but never write it. The compiler checks. *)
external view function get() : uint is
  storage.value
 
(* Stateful functions can return a value too, after their writes. *)
external function swap(const value : uint) : uint is {
  const old : uint := storage.value;
  storage.value := value;
} with old

Try making get write storage:

declared effect is weaker than the body's: external view function `get` writes storage