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

24
LINES

< > BotCompany Repo | #1009173 - Either - union type

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2683L/17K).

sclass Either<A, B> {
  byte which;
  O value;
  
  *() {}
  *(int which, O *value) { this.which = (byte) which; }
  
  bool isA() { ret which == 1; }
  bool isB() { ret which == 2; }
  
  A a() { if (which != 1) _failMe(); ret (A) value; }
  B b() { if (which != 2) _failMe(); ret (B) value; }
  
  A aOpt() { ret which != 1 ? null : (A) value; }
  B bOpt() { ret which != 2 ? null : (B) value; }
  
  void _failMe {
    fail("Either object is of wrong type: " + shortClassName(value));
  }
  
  toString {
    ret "Either" + (isA() ? "A" : "B") + "(" + value + ")";
  }
}

Author comment

Began life as a copy of #1009167

download  show line numbers  debug dex   

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

No comments. add comment

Snippet ID: #1009173
Snippet name: Either - union type
Eternal ID of this version: #1009173/3
Text MD5: 9fe255a383c894d06d431159ee6ebf71
Transpilation MD5: 3b2204f04af8fcb13d98ffd1f28985e7
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-05-23 17:15:47
Source code size: 596 bytes / 24 lines
Pitched / IR pitched: No / No
Views / Downloads: 374 / 2207
Version history: 2 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1009173 & http://1009173.tinybrain.de