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

// TODO: use this instead of #1009173 to save one field. yay!
abstract sclass Either<A, B> {
  bool isA() { false; }
  bool isB() { false; }
  A a() { throw _failMe(); }
  B b() { throw _failMe(); }
  A aOpt() { null; }
  B bOpt() { null; }
  
  abstract O value();
  
  RuntimeException _failMe() {
    fail("Either object is of wrong type: " + shortClassName(value()));
  }
  
  toString {
    ret "Either" + (isA() ? "A" : "B") + "(" + value() + ")";
  }
}

sclass EitherA<A, B> extends Either<A, B> {
  A value;
  
  bool isA() { true; }
  A a() { ret value; }
  A aOpt() { ret value; }
  O value() { ret value; }
}

sclass EitherB<A, B> extends Either<A, B> {
  B value;

  bool isB() { true; }
  B b() { ret value; }
  B bOpt() { ret value; }
  O value() { ret value; }
}

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: 91 / 139
Version history: 9 change(s)
Referenced in: [show references]