Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

45
LINES

< > BotCompany Repo | #1035280 // VarContext backup

JavaX fragment (include)

// VarContext is thread-safe AFTER the first variable has been defined
// or after you call makeThreadSafe(), whichever comes first
sclass VarContext {
  gettable VarContext parent;
  MapSO vars;
  
  *() {}
  *(VarContext *parent) {}
  
  O get(S name) {
    if (containsKey(vars, name))
      ret mapGet(vars, name);
    if (parent != null)
      ret parent.get(name);
    null;
  }
  
  void set aka put(S name, O value) {
    vars = putOrCreateSyncMap(vars, name, value);
  }
  
  AutoCloseable tempSet aka tempPut(S name, O value) {
    initMap();
    ret main tempPut(vars, name, value);
  }
  
  void unset(S name) {
    remove(vars, name);
  }
  
  void printMe {
    pnl(vars);
    print(+parent);
  }
  
  void makeThreadSafe aka initMap() {
    vars if null = syncHashMap();
  }
  
  // mechanism for returning from a script
  
  settable O exitFromScript;
  settable O returnValue;
  bool exiting() { ping(); ret exitFromScript != null; }
}

Author comment

Began life as a copy of #1033977

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035280
Snippet name: VarContext backup
Eternal ID of this version: #1035280/1
Text MD5: 3b590f29956b73c75e64797498dafe4d
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-28 22:42:32
Source code size: 996 bytes / 45 lines
Pitched / IR pitched: No / No
Views / Downloads: 160 / 173
Referenced in: [show references]