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).

1  
sclass Either<A, B> {
2  
  byte which;
3  
  O value;
4  
  
5  
  *() {}
6  
  *(int which, O *value) { this.which = (byte) which; }
7  
  
8  
  bool isA() { ret which == 1; }
9  
  bool isB() { ret which == 2; }
10  
  
11  
  A a() { if (which != 1) _failMe(); ret (A) value; }
12  
  B b() { if (which != 2) _failMe(); ret (B) value; }
13  
  
14  
  A aOpt() { ret which != 1 ? null : (A) value; }
15  
  B bOpt() { ret which != 2 ? null : (B) value; }
16  
  
17  
  void _failMe {
18  
    fail("Either object is of wrong type: " + shortClassName(value));
19  
  }
20  
  
21  
  toString {
22  
    ret "Either" + (isA() ? "A" : "B") + "(" + value + ")";
23  
  }
24  
}

Author comment

Began life as a copy of #1009167

download  show line numbers  debug dex  old transpilations   

Travelled to 18 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ekrmjmnbrukm, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wnsclhtenguj, 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: 612 / 4216
Version history: 2 change(s)
Referenced in: [show references]