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

58
LINES

< > BotCompany Repo | #1012585 // Reified Calculation v2 [OK]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (1845L/13K).

!7

sclass Calculation {
  O f;
  Calculation deferredTo;
  OKOrError result;
  
  *() {}
  *(O *f) {}
  
  void calc() {
    if (done()) ret;
    try {
      result = OKOrError_ok(callF(f));
    } catch e {
      Throwable inner = getInnerException(e);
      if (inner instanceof DeferCalculation)
        deferredTo = inner/DeferCalculation.calculation();
      else
        result = OKOrError_error(e);
    }
  }
  
  bool done() { ret result != null || deferredTo != null; }
  
  toString {
    S s = "Calculation of " + f + ".";
    if (deferredTo != null) s += " Deferred to: " + deferredTo;
    else if (result != null) s += " " + result;
    else s += " Not run";
    ret s;
  }
  
  Calculation resolve() {
    Calculation c = this;
    while (c.deferredTo != null) c = c.deferredTo;
    ret c;
  }
}

p {
  Calculation c = new Calculation(f now);
  print(c);
  c.calc();
  print(c);
  
  c = new Calculation(f fail);
  c.calc();
  print(c);
  
  c = new Calculation(r { throw DeferCalculation(Calculation(f now)) });
  print(c);
  c.calc();
  print(c);
  c = c.resolve();
  c.calc();
  print(c);
}

Author comment

Began life as a copy of #1012583

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1012585
Snippet name: Reified Calculation v2 [OK]
Eternal ID of this version: #1012585/3
Text MD5: 7f158e8e503df1527bed136d1d8f36cd
Transpilation MD5: 720a6987f121845f022041a94aa3fcff
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-12-07 14:37:27
Source code size: 1164 bytes / 58 lines
Pitched / IR pitched: No / No
Views / Downloads: 326 / 776
Version history: 2 change(s)
Referenced in: [show references]