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

37
LINES

< > BotCompany Repo | #1031918 // Either - more compact version

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

Libraryless. Click here for Pure Java version (3231L/18K).

1  
// TODO: use this instead of #1009173 to save one field. yay!
2  
abstract sclass Either<A, B> {
3  
  bool isA() { false; }
4  
  bool isB() { false; }
5  
  A a() { throw _failMe(); }
6  
  B b() { throw _failMe(); }
7  
  A aOpt() { null; }
8  
  B bOpt() { null; }
9  
  
10  
  abstract O value();
11  
  
12  
  RuntimeException _failMe() {
13  
    fail("Either object is of wrong type: " + shortClassName(value()));
14  
  }
15  
  
16  
  toString {
17  
    ret "Either" + (isA() ? "A" : "B") + "(" + value() + ")";
18  
  }
19  
}
20  
21  
sclass EitherA<A, B> extends Either<A, B> {
22  
  A value;
23  
  
24  
  bool isA() { true; }
25  
  A a() { ret value; }
26  
  A aOpt() { ret value; }
27  
  O value() { ret value; }
28  
}
29  
30  
sclass EitherB<A, B> extends Either<A, B> {
31  
  B value;
32  
33  
  bool isB() { true; }
34  
  B b() { ret value; }
35  
  B bOpt() { ret value; }
36  
  O value() { ret value; }
37  
}

Author comment

Began life as a copy of #1009173

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1031918
Snippet name: Either - more compact version
Eternal ID of this version: #1031918/10
Text MD5: acb3663defc80660689d2ef7e085cc9e
Transpilation MD5: 25d26f998464658c5dba8b1eff2b6bc9
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-07-30 23:53:38
Source code size: 813 bytes / 37 lines
Pitched / IR pitched: No / No
Views / Downloads: 94 / 143
Version history: 9 change(s)
Referenced in: [show references]