Skip to content

vizotov/Either-JavaType

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

(If you like this, please consider helping to improve its visibility:

https://twitter.com/fejnartal/status/961133753694289920

https://stackoverflow.com/a/48711573/8699899 )

Implementing Either<Left,Right> in Java

(New Approach - Implemented in this library)

Instantiation

  • Multiples constructors + Parameter Inference + var-args
public final class Either<T1,T2> {
    public enum L { Left }
    public enum R { Right }

    public Either(T1 left , L... _) {/*Omitted*/}
    public Either(T2 right, R... _) {/*Omitted*/}
    /*Omitted*/
}
////////////// In use //////////////
new Either<Integer,String>(2);
new Either<Integer,String>("Text");

Deconstruction

  • Matching classes
public final class IntStringMatcher implements Either.Alternative<Integer,String> {
  @Override public void when(Integer value, L... _) {/*Omitted*/}
  @Override public void when(String  value, R... _) {/*Omitted*/}
}
////////////// In use //////////////
Either<Integer,String> e = new Either<Integer,String>("Text");
e.match(new IntStringMatcher());
  • Matching classes - Closure
public final class MatcherClosure {
  public MatcherClosure(Either<Integer,String> e) { e.match(new IntStringMatcher()); }

  private final class IntStringMatcher implements Either.Alternative<Integer,String> {
    @Override public void when(Integer value, L... _) {/*Omitted*/}
    @Override public void when(String  value, R... _) {/*Omitted*/}
  }
}
////////////// In use //////////////
Either<Integer,String> e = new Either<Integer,String>(0);
new MatcherClosure(e);

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages