Any notion of global variables?

Hi again

Is it possible to define global variables in an intuitive way? For instance, I want to recreate a state machine in a Move module, where module functions can be disabled or enabled depending on a global state.

Thanks again

1 Like

You can simulate a global via a singleton resource as follows:

module UniqueCapability {
  resource T {}

  public grant() {
    let sender: address;
    let unique_holder: address;
    let t: R#Self.T;

    sender = get_txn_sender();
    // address of the person who should hold the unique capability
    unique_holder = 0xb0b;
    assert(move(sender) == move(unique_holder), 77);

    t = T{};
    // an account can have at most one resource of type M.UniqueCapability,
    // and we do not expose any procedures that do move_from<T>
    // this means that only 0xb0b can get the capability, and he can only call
    // grant once


  public show_capability(): &mut R#Self.T {
    let sender: address;
    let t_ref: &mut R#Self.T;

    sender = get_txn_sender();
    t_ref = borrow_global<T>(move(sender));

    return move(t_ref);

  // ... privileged procedures that require a &Self.UniqueCapability to call


It’s not great that you need to hardcode the address 0xb0b in this case. We might consider extending the IR with syntactic sugar for the address of the module publisher (e.g., Self.publisher_address).


Thanks again. Always on the spot :slight_smile:

1 Like